-->
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: OrderBy by attribute of a dependent table
PostPosted: Wed Mar 08, 2006 11:43 am 
Newbie

Joined: Wed Mar 08, 2006 11:12 am
Posts: 16
Hello,
i would need to know if there is any way how to specify OrderBy clause for certain table, where the attribute to be used for sorting is an attribute of a dependent table.
Better would be to give an example. Lets have a db table Task with column priority_id. The column priority_id is filled from a table Priorities (it is a lookup table with 2 columns - id, title). There is of course defined relationship between these 2 tables in Hibernate.
And now when fetching records from the Task table i need to sort them by their priority, but not by the priority_id attribute. I need to use title column of the Priorities table for sorting.
I can't alter the data of the Priorities table, that the order of ids will correspond to the order of titles.

I'm new to Hibernate, so I hope this is not a silly question.

thanx for any hep


Top
 Profile  
 
 Post subject: Re: OrderBy by attribute of a dependent table
PostPosted: Wed Mar 08, 2006 12:03 pm 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
You could do that by using a criteria query and using a Alias for your priority association.
I suppose your TaskClass looks like that...
public class Task{
Long id;
Priority priority;
....
}
public class Priority{
Long id;
String title;
....
}




Code:
Criteria crit = session.getSession().createCriteria(Task.class).createAlias("prio",priority);
crit.add(Restrictions.eq("",new Long(24))); // do your query...
cirt.addOrder(Order.asc("prio.title"));
crit.list(); //or cirt.uniqueResult();


I'm not sure whether you need the alias, in this case of association I guess you can do it without. I like to use the alias in this kind of queries.....


simon


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 5:56 am 
Newbie

Joined: Wed Mar 08, 2006 11:12 am
Posts: 16
thanx a lot, that helped in the case which i described.
But it doesn't work in the case of more deep references e.g. over one table.

Example:
Table Task and dependent table Participant and its dependent table UserRole.
And now i want to display all tasks also with the atribute roleName (column of the UserRole table) and sort by this attribute. Is it possible to do so with the same or similar technique? I was not sucessfull with that...

thanx for any hint


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 8:15 am 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
djanca wrote:
thanx a lot, that helped in the case which i described.
But it doesn't work in the case of more deep references e.g. over one table.

Example:
Table Task and dependent table Participant and its dependent table UserRole.
And now i want to display all tasks also with the atribute roleName (column of the UserRole table) and sort by this attribute. Is it possible to do so with the same or similar technique? I was not sucessfull with that...

thanx for any hint


Ok, I guess I know what you wanna do, the following code does sort the result by the roleName attribtue you described above.

Code:
Criteria crit = session.getSession().createCriteria(Task.class).createAlias("participant","part").createAlias("part.userrole", "role");
crit.add(Restrictions.eq("",new Long(24))); // do your query...
cirt.addOrder(Order.asc("role.roleName"));
crit.list(); //or cirt.uniqueResult();



yours simon


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.