-->
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.  [ 2 posts ] 
Author Message
 Post subject: Order by using Criteria
PostPosted: Fri Apr 20, 2007 5:48 am 
Newbie

Joined: Fri Apr 20, 2007 5:30 am
Posts: 2
Hi,

I have 2 tables named Forms and FormPages. Forms have one to many relationship to FormPages table. The Forms entity can have multiple FormPages entities in it. I need to retrieve all Forms with the FormPages sorted by a key in the FormPages table named Pageorder . I have written the following code for that:

List formslist =HibernateUtil.getCurrentSession().createCriteria(Forms.class)

.createAlias("formpageses","formpages")

.addOrder(Order.asc("formpages.Pageorder"))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)

.list();


But this doesn’t sort the Formpages collection based on the Pageorder field in Formpages.

Does anyone know how to do this?

Thanks in advance

Jency


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2007 8:24 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
The ResultTransformer will break the collection sorting. Think about the resultset from the query (sorted on B.COL_D):
Code:
A.COL_A     A.COL_B    B.COL_C    B.COL_D
1           1          1          1
1           1          5          2
1           1          18         3
2           6          7          4
2           6          9          5

When the ResultTransformer(DISTINCT_ROOT_ENTITY) is used, it will only return A entities, the collections will be unfiltered and unsorted:
Code:
A.COL_A    A.COL_B
1          1
2          6

In short, use the ALIAS_TO_ENTITY_MAP ResultTransformer. It's more of a pain to work with, but it will get you what you want.


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