-->
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: sorting based on field in associated object?
PostPosted: Tue Sep 11, 2007 5:18 pm 
Newbie

Joined: Tue Sep 11, 2007 4:48 pm
Posts: 2
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 11, 2007 8:12 pm 
Newbie

Joined: Wed Sep 05, 2007 6:34 pm
Posts: 13
Hi Steven,

I think you can nest criteria (or criterium?... eh) like this:

Code:
Criteria query = session.createCriteria(Person.class);
query
   .createCriteria("recruitedBy")
   .addOrder(Order.asc("loginName"));


I currently have something similar. To match a particular symbol from PseudoPotential.getAtom().getSymbol(), I do the following:

Code:
Criteria criteria = getSession().createCriteria(PseudoPotential.class);
criteria
  .createCriteria("atomObj")
  .add(Expression.eq("symbol", search.getSymbol()));


Section 15.4 from the following may help...
http://www.hibernate.org/hib_docs/v3/re ... teria.html

Hope this helps!

-- Dan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 12, 2007 10:47 am 
Newbie

Joined: Tue Sep 11, 2007 4:48 pm
Posts: 2
Dan --

Thanks, that works perfectly.

Steven


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.