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 :)

adaniluk
  • 321
  • 1
  • 5
  • 18
  • 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? – dinukadev Jun 02 '13 at 12:22
  • I tried with criteria and authorCriteria. – adaniluk Jun 02 '13 at 12:23
  • Can you share your table structure for the same please? – dinukadev Jun 02 '13 at 12:24
  • Have to tried printing the generated SQL to see if the query is what you would expect? http://stackoverflow.com/questions/1710476/print-query-string-in-hibernate-with-parameter-values – Brent Worden Jun 02 '13 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 – dinukadev Jun 02 '13 at 12:31
  • I tried `DetachedCriteria criteria = DetachedCriteria.forClass(Article.class); criteria.createAlias("author", "author_id"); criteria.add(Restrictions.eq("author_id.id", ((User)session.getAttribute("user")).getId()));` the same result – adaniluk Jun 02 '13 at 12:39
  • Can you print out the sql generated by hibernate by enabling show sql?also your association here is @OnetoOne is it? – dinukadev Jun 02 '13 at 12:48
  • @ManyToOne, I can't post SQL 'cause it's too long, Writer may have many articles – adaniluk Jun 02 '13 at 12:54
  • I don't see how the SQL should be too long. It's just a simple SQL query, not a whole book. – JB Nizet Jun 02 '13 at 12:59
  • Did you try to use hardcoded value in `Restrictions.eq("id", ...`? – John Doe Jun 02 '13 at 13:01
  • Have you debugged your code to ensure a valid parameter is available within (User)session.getAttribute("user")? – dinukadev Jun 02 '13 at 13:01
  • @John Doe with hardcoded the same result – adaniluk Jun 02 '13 at 13:05
  • @JB Nizet question validation doesn't allow to post to much code – adaniluk Jun 02 '13 at 13:06
  • @donatello: we're talking about a single SQL query here. That's not the definition of "too much code". – JB Nizet Jun 02 '13 at 13:07

1 Answers1

1

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

List<Article> articles = articleManager.findArticleByCriteria(criteria);
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • in comments I wrote that I tried citeria and authorCriteria both with the same result – adaniluk Jun 02 '13 at 12:59
  • 1
    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. – JB Nizet Jun 02 '13 at 13:00
  • 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. – JB Nizet Jun 02 '13 at 13:25
  • The user id in session is the same as the author_fk field value in article table – adaniluk Jun 02 '13 at 13:28
  • 2
    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()`. – JB Nizet Jun 02 '13 at 13:30