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