-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Problem with query expressed as criteria...
PostPosted: Tue Oct 16, 2007 12:38 pm 
Newbie

Joined: Sun Sep 11, 2005 1:16 am
Posts: 5
The following two methods return different number of objects. The one done by criteria returns duplicates objects for one row in the table about in the middle of the table. I do not understand why these are returning different results.


Code:
   public List<Category> findCategoriesSortedByLft()
   {
      return (List<Category>) getHibernateTemplate().execute(new HibernateCallback()
      {
         public Object doInHibernate( Session session )
         {
            Criteria criteria = session.createCriteria(Category.class);
            criteria.addOrder(Order.asc("lft"));

            return criteria.list();
         }
      });
   }
   
   public List<Category> findCategoriesSortedByLft1()
   {
      String query =
         "from Category c " +
         "order by c.lft";
      
      return (List<Category>) getHibernateTemplate().find(query);
   }


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 12:51 pm 
Regular
Regular

Joined: Mon Aug 07, 2006 6:22 pm
Posts: 67
It might help to see what query it's actually generating, by turning on SQL logging, by setting the following properties:

* hibernate.show_sql
* hibernate.format_sql
* hibernate.use_sql_comments


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 2:36 pm 
Regular
Regular

Joined: Mon Aug 07, 2006 6:22 pm
Posts: 67
After doing a little more reading in the new Hibernate book, I realize there is at least one significant difference between querying with Criteria and dynamic queries.

Apparently Criteria queries use any global fetching strategy defined in the mapping configuration for the class, and dynamic queries "ignore" it. If you have a join fetch set in that class, then it's possible you could see this behavior.

Thinking about this, I find this a little confusing, as I have a set of classes that are all set with eager fetching with subselect on the "set" attributes, and I use a very simple dynamic query on the main class, and that appears to respect the global fetching strategy, causing all the "set" attributes to be eagerly loaded with subselects.


Top
 Profile  
 
 Post subject: Criteria does left join, hql inner join...
PostPosted: Tue Oct 16, 2007 4:38 pm 
Newbie

Joined: Sun Sep 11, 2005 1:16 am
Posts: 5
Category is related to another entity. After looking at the queries that are generated I can see that the one based on criteria is doing a left join, and the one based on hql is doing an inner join.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.