-->
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.  [ 5 posts ] 
Author Message
 Post subject: Order by columns of child tables
PostPosted: Mon Sep 15, 2008 8:52 pm 
Newbie

Joined: Mon Sep 15, 2008 8:30 pm
Posts: 7
Hi ,

I need to do order by a column of child table.I tried with the following code, but it's not giving me the desired result.

I have two tables ACCOUNT_FILTER(parent) and ACCOUNT_CONFIG_FILTER(child) and am trying to do the asc order on the column 'GENRE' of child table.

Criteria criteria = getSession().createCriteria(AccountFilter.class);
criteria.add(Restrictions.eq("id",accountFilterId));
criteria.createAlias("accountConfigurationFilters", "acf");
criteria.addOrder(Order.asc("acf.genre"));

Per the above code , I should get the 'genre' values in the alphabetical order. But I am getting in the following order.

Reggae
Holiday
Pop
Comedy
R B
Chillout
Rock
Miscellaneous
Samba
New Age
null
World Music
Rap/Hip Hop
Metal
Soundtrack
Musical
Dance
Latin
Alternative
Adult Alternative
Adult Contemporary
Alternative Rock
Electronica

Someone please tell me, how to do the order by columns of child table. anything wrong with my code?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2008 10:11 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
create a new criteria on the associated entity, and return your results from that one, not from the main query

Code:
Criteria criteria = getSession().createCriteria(AccountFilter.class);
criteria.add(Restrictions.eq("id",accountFilterId));

Criteria subCriteria = criteria.createCriteria("accountConfigurationFilters", "acf");
subCriteria.addOrder(Order.asc("acf.genre"));

return subCriteria.list();

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2008 11:12 pm 
Newbie

Joined: Mon Sep 15, 2008 8:30 pm
Posts: 7
Diaz,

Thanks for quick reply. I tried as you suggested, but no luck. Still I am getting the values in random order.

Below query that is generated by the Hibernate looks fine for the below code . It's doing the order by genre ASC. I am not sure why I am not getting the ordered values in my Java class.

Criteria criteria = getSession().createCriteria(AccountFilter.class);
criteria.add(Restrictions.eq("id",accountFilterId));
criteria.createAlias("accountConfigurationFilters", "acf");
criteria.addOrder(Order.asc("acf.genre"));

SELECT this_.account_filter_id AS account1_13_1_,
acf1_.major_label AS major5_14_0_, acf1_.label AS label14_0_,
acf1_.genre AS genre14_0_, ,
acf1_.account_filter_id AS account25_14_0_
FROM dmd.account_filter this_ INNER JOIN dmd.account_configuration_filter acf1_
ON this_.account_filter_id = acf1_.account_filter_id
WHERE this_.account_filter_id = ?
ORDER BY acf1_.genre ASC


If I put the criteria conditions i.e like, or, and etc on child table columns then I am getting the results fine. But. order by on the child columns is not working with criteria.createCriteria OR criteria.createAlias.


Help pleaseeeeeeee..


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2008 11:22 pm 
Newbie

Joined: Mon Sep 15, 2008 8:30 pm
Posts: 7
Here I am trying to fetch the parent object and the corresponding child objects (one-to-many association) order by on the child table column.

I tried with following code too.

List<AccountFilter> ac = (List<AccountFilter>) getSession().createCriteria(AccountFilter.class)
.add(Restrictions.eq("id",accountFilterId))
.createCriteria("accountConfigurationFilters","acf")
.addOrder(Order.asc("acf.genre")).list();

when I do, ac.getChildRecords(), they are not coming in asc order of 'genre' column.


Is it possible to get the child records order by a specific column ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 16, 2008 12:04 am 
Newbie

Joined: Mon Sep 15, 2008 8:30 pm
Posts: 7
It's not possible to order by a column on the child table. Find more in the below link

http://forum.hibernate.org/viewtopic.ph ... 187428d902


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