-->
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.  [ 5 posts ] 
Author Message
 Post subject: HQL with join fetch ignores order by clause
PostPosted: Thu Nov 10, 2005 10:20 am 
Newbie

Joined: Thu Nov 10, 2005 10:05 am
Posts: 3
I use this simple HQL query to get a list of countries and the states for each one of them, in alphabetical order.

Code:
       
String queryStr = "from Country c left join fetch c.states s where c.active=:active and s.active=:active order by c.name, s.name";
List<Country> countryList =   getSession().createQuery(queryStr).setBoolean("active", isActive).list();
return new LinkedHashSet<Country>(countryList);


the "translated" sql query produced by this hql returns a perfectly ordered result set, according to the criteria I gave, BUT the LinkedHashSet where I place my results is only ordered by country.name and each country contains sets of states that are randomly ordered. That is, the second order by clause(state.name) seems to be ignored.

Thanks in advance for any useful idea you might have on this


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 11, 2005 12:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
what is the type of the Country.states collection?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 11, 2005 5:30 am 
Newbie

Joined: Thu Nov 10, 2005 10:05 am
Posts: 3
I think I got your point there. The type of Country.state was java.util.Set, so it couldn't keep a sorted order. Changed it to LinkedHashSet (as you can see in the code below
Code:
public abstract class BaseCountry  implements Comparable, Serializable {
   // primary key
   private java.lang.Integer id;

   // fields
   private java.lang.String name;
......
   // collections
   private java.util.LinkedHashSet states;
        //accessors
   public java.lang.String getName () {
      return name;
   }

   public void setName (java.lang.String name) {
      this.name = name;
   }
.........
   public java.util.LinkedHashSet getStates () {
      return states;
   }
   public void setStates (java.util.LinkedHashSet states) {
      this.states = states;
   }

}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 11, 2005 5:32 am 
Newbie

Joined: Thu Nov 10, 2005 10:05 am
Posts: 3
Sorry my previous post was unfinished.
Changing the code resulted to a class cast exception:
Code:
Hibernate: select country0_.countryId as countryId0_, states1_.stateId as stateId1_, country0_.shortName as shortName7_0_, country0_.name as name7_0_, country0_.comments as comments7_0_, country0_.isActive as isActive7_0_, country0_.isDefault as isDefault7_0_, country0_.editDate as editDate7_0_, country0_.editUser as editUser7_0_, states1_.shortName as shortName47_1_, states1_.name as name47_1_, states1_.comments as comments47_1_, states1_.isActive as isActive47_1_, states1_.isDefault as isDefault47_1_, states1_.editDate as editDate47_1_, states1_.editUser as editUser47_1_, states1_.countryId as countryId47_1_, states1_.countryId as countryId0__, states1_.stateId as stateId0__ from country country0_ left outer join state states1_ on country0_.countryId=states1_.countryId where country0_.isActive=true and states1_.isActive=true order by country0_.name, states1_.name
Exception in thread "AWT-EventQueue-0" org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of wresponse.domain.Country.setStates
   at org.hibernate.tuple.PojoTuplizer.setPropertyValuesWithOptimizer(PojoTuplizer.java:217)
   at org.hibernate.tuple.PojoTuplizer.setPropertyValues(PojoTuplizer.java:187)
   at org.hibernate.persister.entity.BasicEntityPersister.setPropertyValues(BasicEntityPersister.java:2945)
   at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:115)
   at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:791)
   at org.hibernate.loader.Loader.doQuery(Loader.java:689)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:221)
   at org.hibernate.loader.Loader.doList(Loader.java:1858)
   at org.hibernate.loader.Loader.list(Loader.java:1842)
   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:407)
   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:273)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:850)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
   at wresponse.sqltest.SqlTesterForm.execute(SqlTesterForm.java:49)
   at wresponse.sqltest.SqlTesterForm.access$000(SqlTesterForm.java:19)
   at wresponse.sqltest.SqlTesterForm$1.actionPerformed(SqlTesterForm.java:33)
   at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
   at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
   at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
   at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
   at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
   at java.awt.Component.processMouseEvent(Component.java:5488)
   at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
   at java.awt.Component.processEvent(Component.java:5253)
   at java.awt.Container.processEvent(Container.java:1966)
   at java.awt.Component.dispatchEventImpl(Component.java:3955)
   at java.awt.Container.dispatchEventImpl(Container.java:2024)
   at java.awt.Component.dispatchEvent(Component.java:3803)
   at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
   at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
   at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
   at java.awt.Container.dispatchEventImpl(Container.java:2010)
   at java.awt.Window.dispatchEventImpl(Window.java:1766)
   at java.awt.Component.dispatchEvent(Component.java:3803)
   at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
   at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
   at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Caused by: net.sf.cglib.beans.BulkBeanException: org.hibernate.collection.PersistentSet
   at wresponse.domain.Country$$BulkBeanByCGLIB$$5dd4bf52.setPropertyValues(<generated>)
   at org.hibernate.tuple.PojoTuplizer.setPropertyValuesWithOptimizer(PojoTuplizer.java:214)
   ... 39 more
Caused by: java.lang.ClassCastException: org.hibernate.collection.PersistentSet
   ... 41 more


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 11, 2005 11:11 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
right. Hibernate is trying to inject a Set, which is not castable to LinkedHashSet. You need to tell Hibernate in the mapping that this will be an ordered set:
Code:
<set name="states" order-by="name" ...>
...
</set>


Enter a feature request into JIRA for Hibernate to use an ordered set "behind the scenes" when a fetched set is ordered in an HQL query. Not quite sure of the feasability of that yet, but it be nice to do.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.