I'm trying to work out a query that will sort its results based on the value of a field in an object associated with the objects returned by the query. The model classes are these:
Code:
public class Person {
private Long id;
private User recruitedBy;
}
public class User {
private Long id;
private String loginName;
}
Now I want a list of Person objects, sorted by the loginName of the User object that is the value of the Person's recruitedBy field (person.recruitedBy.loginName).
The relevant parts of the .hbm.xml files:
Code:
<class
name="Person"
table="Person">
<id name="id" column="person_key" type="long">
<generator class="native" />
</id>
<many-to-one name="recruitedBy"
column="recruitedBy"
class="User"
cascade="save-update"/>
</class>
<class
name="User"
table="User">
<id name="id" column="user_key" type="long">
<generator class="native" />
</id>
<property name="loginName" column="login" unique="true" length="100"/>
</class>
My first try was this:
Code:
Criteria query = session.createCriteria(Person.class);
query.addOrder(Order.asc("recruitedBy.loginName"));
This didn't work: there is no 'recruitedBy.loginName' field in the Person class.
At first it looked like Session.createFilter would help, but that needs a persistent object with a collection field. In my case the list of Person objects is not a field of another object, so createFilter won't work for me.
If anyone has any advice, it would be much appreciated.
I'm using Hibernate 3.2.5. At this point in the project, it's not practical to change either the model or the database tables.
Thanks in advance.
Steven Gollery