-->
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: Sort Hibernate Criteria Query Results??
PostPosted: Tue Nov 27, 2007 7:44 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
I have Profile class that has two fields Person, Status.

class Profile{
private Person person;
private Status status;
}

class Person {
private String firstName;
private String lastName;
}

class Status{
private String status;
}

My requirement is to sort the results of Profile on Person.lastName. I am able to sort them on Person or Status. I have done on Status. Here is the HQL:


List _results = (List)session.createCriteria(Profile.class) .addOrder(Order.asc("status"))
.list();

Is there any option with HQL and Hibernate to add the sorting feature for this query on Person.lastName field.

If not, my alternative is to read the objects from _results List and sort them and store in a sorted results List.

TIA
Shyam


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 29, 2007 8:30 am 
Regular
Regular

Joined: Mon Jan 22, 2007 10:32 am
Posts: 101
try this

List _results = (List)session.createCriteria(Profile.class)
.createAlias("person","person").addOrder(Order.asc("person.lastName"))
.list();

_________________
Please rate this post if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 29, 2007 4:22 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
Thanks. It worked :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 12, 2007 4:18 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
I have weird problem. Here is my java class

public Profile{
public Person person;
public Person personHead;
public Person DeptHead;
}

Now when I search for the following

List _results = (List)session.createCriteria(Profile.class)
.createAlias("person","person")
.addOrder(Order.asc("person.lastName"))
.list();

Error message
**************
SEVERE: ORA-00904: "PERSON1_"."LASTNAME": invalid identifier
Query-->select * from T_PROFILE this_ order by person1_.lastName desc
Where the following two works fine.
------------------------------------------------------------------------------
List _results = (List)session.createCriteria(Profile.class)
.createAlias("personHead","person")
.addOrder(Order.asc("person.lastName"))
.list();

Hibernate: select * from T_PROFILE this_ inner join T_PERSON person1_ on this_.personHead=person1_.id
order by person1_.lastName asc



List _results = (List)session.createCriteria(Profile.class)
.createAlias("DeptHead","person")
.addOrder(Order.asc("person.lastName"))
.list();
This works fine.
-------------------------------------------------------------------------------

I have tried this. Still the same error message is coming:

List _results = (List)session.createCriteria(Profile.class)
.createAlias("person","per")
.addOrder(Order.asc("per.lastName"))
.list();

Is there any problem with createAlias method? It is giving error message when I give same name for alias and property?

Thanks


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.