-->
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.  [ 7 posts ] 
Author Message
 Post subject: Order by attribute in Joined Class
PostPosted: Mon Oct 13, 2008 5:12 pm 
Newbie

Joined: Wed Sep 10, 2008 2:31 pm
Posts: 16
Hello,

a Person has a Company (id, name...)

The person mapping has a many-to-one with company (joined by id)

I'm using detached criteria and I do Order.asc("companyInfo.name")

(all the variable names and such are legit and working)

I have tried this on variables that are in the mapping, but not in a join (blah.foo) and they work well, but for the joined class, this has not worked at all.

I get:

Code:

org.hibernate.QueryException: could not resolve property: companyInfo.name of: blah.blah.Person

[/code]


I would permanently set this in the mapping, but it needs to change from ascending to descending (and has pagination so I just can't retrieve all and order then...). Is this possible?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 14, 2008 2:39 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Check the mapping file for the blah.blah.Person class and make sure that the <many-to-one> to Company class is named "companyInfo". Eg.

Code:
<many-to-one name="companyInfo" class="blah.blah.Company" ..../>


If it still doesn't work, post your code and mapping documents.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 14, 2008 8:59 am 
Newbie

Joined: Wed Sep 10, 2008 2:31 pm
Posts: 16
I'm on hibernate 3.0

Company
Code:

<hibernate-mapping package="blah.blah" schema="BIZ">
   <class name="Company" table="VW_COMPANY" lazy="false">
      
      <id name="id" column="COMPANY_ID" type="string" />
      <property name="name" column="NAME" type="string"/>
      <property name="abbr" column="ABBREVIATION" type="string"/>
   </class>      
</hibernate-mapping>



Person


Code:
<hibernate-mapping package="blah.blah">
   <class name="blah.Person"
      table="BIZ.PERSON_INFORMATION" lazy="false" mutable="false">
      
      <composite-id name="personPK" class="PersonPK">
         <key-property name="personNumber" column="PERSONNUMBER" type="string" />
         <key-property name="businessCode" column="BUSINESSCD" type="string" />
      </composite-id>

      <property name="lastName" column="LASTNAME" type="string" />   
      <property name="firstName" column="FIRSTNAME" type="string" />
      <property name="phoneNumber" column="PHONENUMBER" type="string" />

      
      <many-to-one name="companyInfo" update="false" insert="false" column="BUSINESSCD" class="Company" />
      
   </class>
</hibernate-mapping>




Ordering code:


Code:
DetachedCriteria mainQuery = crit;
String order = "companyInfo.name";
Session session = this.getSession();
         Transaction txn = session.beginTransaction();
         //Have to orderby a certain list otherwise things may get jumbled up with pagination
         if(asc){
            mainQuery.addOrder(Order.asc(order));
         } else {
            mainQuery.addOrder(Order.desc(order));   
         }
         results = mainQuery.getExecutableCriteria(session).setFirstResult(page*pageSize)
                        .setFetchSize(pageSize).setMaxResults(pageSize).list();
         txn.commit();




Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 14, 2008 9:52 am 
Newbie

Joined: Wed Sep 10, 2008 2:31 pm
Posts: 16
I've also noticed it doesn't really look like the sql code generated by hibernate doesn't really do a join... I explicitly now set the join in the many-to-one mapping (fetch="join"), but seems to be looking the exact same...


Code:

select ... from BIZ.PERSON_INFORMATION this_,
BIZ.VW_COMPANY company2_




Weird there is no explicit join in the sql...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 14, 2008 2:10 pm 
Newbie

Joined: Wed Sep 10, 2008 2:31 pm
Posts: 16
Thanks for all the help, but I've given up on it. Seems the way hibernate will do pagination is store the whole bit in memory and then sort it and then bring back only some of the results, which is absolutely worthless because I can do that with a list.

Sometimes the deeper I dive into hibernate the less it lives up to the hype...

Thanks to this site for helping out:
http://java.dzone.com/articles/hibernat ... page=0%2C0


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 14, 2008 5:52 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I am not very familiar with Criteria queries, but I find it hard to believe that a feature that is very easy with HQL has been left out of the criteria API. Have you tried the technique used for filtering on the property of an associated entity? There are some code examples at http://www.hibernate.org/hib_docs/v3/re ... tions.html

Maybe you have to assign an alias to the companyInfo association first.
Something similar to this if using the example from the document above.

Code:
List cats = sess.createCriteria(Cat.class)
    .createAlias("mate", "mt")
    .addOrder( Order.asc("mt.name") )
    .list();


or with your code:

Code:
mainQuery.createAlias("companyInfo", "ci")
   .addOrder(Order.asc("ci.name"));


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 8:40 am 
Newbie

Joined: Wed Sep 10, 2008 2:31 pm
Posts: 16
Yeah, that worked very well, straight out of the box :). It's too bad hibernate still does the pagination in memory.

Thanks again


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