-->
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.  [ 3 posts ] 
Author Message
 Post subject: Order by sub property
PostPosted: Sun Jul 03, 2005 7:18 pm 
Newbie

Joined: Sun Jul 03, 2005 8:46 am
Posts: 1
Location: Sydney
Hibernate version:3.0.5

I have 2 classes:
Code:
public class Item {
   protected Integer itemId;
   protected State state;
   
   //getters and setters
}

public class State {
   protected Integer stateId;
   protected String name;
   protected String description;
   protected Integer naturalOrder;
   
   //getters and setters...
}

State is mapped as many-to-one in Item:
Code:
<hibernate-mapping package="com.company.model">
   <class name="Item" table="ITEMS">
      <id name="id" column="ITEM_ID" type="integer">
         <generator class="sequence" />
      </id>
      <many-to-one name="state" column="ITEM_STATE_ID" class="State" not-null="false" lazy="false"/>
      <!--
      ...
      -->
   </class>
</hibernate-mapping>

<hibernate-mapping package="com.company.model">
   <class name="State" table="STATES">
      <id name="id" column="STATE_ID" type="integer">
         <generator class="sequence" />
      </id>
      <property name="name" column="STATE_NAME" type="string"/>
      <property name="description" column="description" type="string" />
      <property name="naturalOrder" column="n_order" type="integer" />
   </class>
</hibernate-mapping>

What I am trying to achieve is to load all Items ordered on the state.naturalOrder. Is this possible?

I have tried:
Code:
Criteria crit = session.createCriteria(Item.class).setCacheable(false).setMaxResults(20).setFirstResult(1);
crit = crit.addOrder(Order.asc("state.naturalOrder"));


But all I get is a hibernate exception stating that the property "state.naturalOrder" cannot be found.

I guess if this cannot be acheived I'll try a bi-directional join and call from the State class instead, but if I've overlooked something can someone let me know.

Anthony


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 9:42 am 
Beginner
Beginner

Joined: Wed Nov 24, 2004 10:54 am
Posts: 48
aikeda, did you ever get this figured out? I am having the same problem


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 12:03 pm 
Newbie

Joined: Fri Oct 31, 2003 3:14 pm
Posts: 12
Location: New York, NY
You might want to try:
Code:
Criteria crit = session.createCriteria(Item.class).setCacheable(false).setMaxResults(20).setFirstResult(1);
Criteria state = crit.createCriteria("state");
state.addOrder(Order.asc("naturalOrder"));


I know I've recently done some order by sub-properties this way.

Hope that helps,
Patrick[/quote]


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