-->
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.  [ 4 posts ] 
Author Message
 Post subject: ordering by related tables with Criteria
PostPosted: Fri Dec 12, 2008 3:55 am 
Newbie

Joined: Thu Nov 27, 2008 9:05 pm
Posts: 9
Hi folks!

I'm wondering how to build a little complex order query with criteria.

Criteria c = ....
c.addOrder( Order.desc("id")) // it works fine.
c.addOrder( Order.desc("contactinfo")) // it works fine.
c.addOrder( Order.desc("contactinfo.name")) // it doesnt and it gives me:

org.hibernate.QueryException: could not resolve property: contactinfo.name of: ...

Any Idea?? I have reviewed my hbm and contactinfo exists and have a name property.

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2008 9:53 pm 
Newbie

Joined: Tue Dec 16, 2008 9:18 pm
Posts: 1
Location: Portland, OR
I have the same question.

(I presume you have one object containing another object via a table join.)

Also, is there a similar way to specify a Criteria Projection, using this kind of "object.field" notation?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2008 4:16 am 
Newbie

Joined: Thu Nov 27, 2008 9:05 pm
Posts: 9
I got it!

as you can see in [1] you have to made an association, creating a new Criteria, an then adding new addOrder methods.

List cats = sess.createCriteria(Cat.class)
.add( Restrictions.like("name", "F%") )
.createCriteria("kittens")
.add( Restrictions.like("name", "F%") )
.addOrder( Order.desc("name"))
.list();


[1] http://www.hibernate.org/hib_docs/refer ... tions.html


:)
David CanĂ³s


Top
 Profile  
 
 Post subject: Re: ordering by related tables with Criteria
PostPosted: Thu Jun 04, 2009 9:14 am 
Newbie

Joined: Thu Jan 15, 2009 9:54 am
Posts: 12
Hi
c.addOrder( Order.desc("contactinfo")) // this will work 100%
c.addOrder( Order.desc("contactinfo.name")) // this will not work 100%.

reason is very simple.

property NAME is not the joining key or foreign key.
you can just order by foriegn key properties.
like contactinfor.employeeId

your mapping file will not be joining with the ContactInfo mapping file on Name property.
you can only order by those properties which are there in the mapping file.

for example:

here is my employee mapping file.

<class name="EmployeeDTO" mutable="false" table="DB2.WRKFRC_CRNT2_VW">
<id name="empId" column="WRKFRC_ID">
<generator class="native" />
</id>
<property name="emailId" column="EML_ADDR_TXT" />
<property name="empName" formula=" concat( RTRIM(PRMY_LAST_NM), concat(', ',RTRIM(PRMY_FRST_NM)))" />
<property name="formalLdrId" column="FORMAL_LDR_ID" />
<property name="empFullNameWithMiddleInitial"
formula="concat( RTRIM(PRMY_LAST_NM), CONCAT(' ',CONCAT(RTRIM(PRMY_FRST_NM),concat(' ',RTRIM(PRMY_MID_NM)))))" />
<many-to-one name="department" class="DepartmentDTO" column="DEPT_ID" update="false" insert="false" lazy="false" />
<many-to-one name="manager" class="ManagerDTO" column="FORMAL_LDR_ID" update="false" insert="false" lazy="false" />
<one-to-one class="AddressDTO" name="address" foreign-key="TAX_ID"></one-to-one>
<join table="DB2.RAMS_WRKFRC_DTL_VW">
<key column="WRKFRC_ID" />
<property name="userId" column="USER_ID" />
</join>
</class>

Code:
Criteria crt = session.createCriteria(EmployeeDTO.class);

now, order by Department.id will work.
but order by Department.Name will not work.

order by AddressDTO will work,
order by AddressDTO.id will work
but order by AddressDTO.zipCode will not work.

thanks,

Abid,

you can mail me for further clarifications.


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