Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sorting collection on fields of associated object?

 
Steven Gollery
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble coming up with a Criteria query that allows me to sort a list of objects based on the value of a field in another object that the objects being displayed have a reference to. Here's an example to clarify what I mean.

In my model, I have a Recruiter class:

public class Recruiter {
private Long id;
private String loginName;
}

There is also a Person class which associates a Person to a Recruiter:

public class Person {
private Long id;
private Recruiter recruitedBy;
private String lastName;
private String firstName;
}

In the GUI, there is a table that shows the Person's first and last names, and the login name of the Recruiter (person.recruitedBy.loginName).

The customer would like to be able to sort a list of Persons by the loginName of the recruiter, but I haven't been able to come up with a query that does that. This doesn't work:

query.addOrder(Order.asc("recruitedBy.loginName"));

At first, session.createFilter looked promising, but all the examples assume that I have a persistent object that has a lazy-loaded collection, like this:

session.createFilter( collection, "order by ...." ).list();

In my case there is no collection until I execute the query, so it doesn't look like createFilter will work for me.

If anyone has a pointer to a solution, I'd appreciate any help. Thanks in advance.

Steven Gollery
 
Steven Gollery
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it: just needed a nested criteria.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic