2

problem is that I get empty result on query when I should get some elements. here's the code:

DetachedCriteria criteria = DetachedCriteria.forClass(Article.class);
DetachedCriteria authorCriteria = criteria.createCriteria("author");
authorCriteria.add(Restrictions.eq("id",((User)session.getAttribute("user")).getId()));
List<Article> articles = articleManager.findArticleByCriteria(criteria);


@Entity
@Table(name = "ARTICLES")
public class Article {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Integer id;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "author_fk")
    Writer author;
    @Column(length = 10000)
    String content;
    String title;
    @Transient
    String shortContent;
    ... 
}

I expect to get Articles of specific author.

@edit

        id  content                                                  date           flagEditor  flagWriter  title   author_fk   editor_fk
        30  nweINSERT INTO `WRITERS` (`USER_ID`) VALUES<br>(9)...   2013-06-02 13:14:55     0   0   new     9   NULL
        31  INSERT INTO `WRITERS` (`USER_ID`) VALUES<br>(9);IN...   2013-06-02 13:20:04     0   0   dsfsafadsf  9   NULL
        32  sdf((User)session.getAttribute("user"))((User)sess...   2013-06-02 13:35:33     0   0   frefds  9   NULL

Hibernate SQL: http://pastebin.com/yfPz6aDb

ok I found problem:

public List<Article> findByCriteria(DetachedCriteria criteria){
    List<Article> articles = null;
    articles = criteria.getExecutableCriteria(HibernateUtil.getSession()).list();
//return value wasn't assignet do articles
    return articles;


}

Thanks all for commitment :)

14
  • I believe you should be passing your root criteria which is "criteria" and not "authorCriteria" to fetch the details. Or was this a typo from your side? Commented Jun 2, 2013 at 12:22
  • I tried with criteria and authorCriteria. Commented Jun 2, 2013 at 12:23
  • Can you share your table structure for the same please? Commented Jun 2, 2013 at 12:24
  • Have to tried printing the generated SQL to see if the query is what you would expect? stackoverflow.com/questions/1710476/… Commented Jun 2, 2013 at 12:28
  • Are you trying to match the "id" of the author table? i think the criteria is taking the id of the article table. You will have to create an alias for the author using createAlias("author", "auth") and then use Restrictions.eq("auth.id",((User)session.getAttribute("user")).getId()) in your query Commented Jun 2, 2013 at 12:31

1 Answer 1

1

You're passing the author criteria instead of passing the root criteria. The code should be:

List<Article> articles = articleManager.findArticleByCriteria(criteria);
Sign up to request clarification or add additional context in comments.

5 Comments

in comments I wrote that I tried citeria and authorCriteria both with the same result
Then you probably don't have any article in database having the an author with the ID of the user stored in the session. If you posted the contents of both tables, the value of the user ID in session, and the SQL query, in a readable format, it would be easier to help.
My idea is that you don't have any book in the table with the author identified by the ID of the user in the session. Debug to see what the user ID is. Check that a writer with this ID exists. Check that a book with this author_fk exists. If those rows exist, check that they have been committed.
The user id in session is the same as the author_fk field value in article table
And do you have a writer with this ID in the writer table? Are you sure you're talking to the right database/schema? What heppens when you execute the same SQL query in your database browser tool? Maybe the code that executes the query is wrong. Show us the code of articleManager.findArticleByCriteria().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.