-->
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: Is there a way to put an Order object in a Criterion list?
PostPosted: Fri Jul 11, 2008 9:41 am 
Newbie

Joined: Fri Jul 11, 2008 9:37 am
Posts: 6
I have been trying to figure out if this is possible, but doesnt look like it since the only thing that addOrder returns is a Criteria object.

thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 11, 2008 10:40 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Here's an example of how I use the HibernateCriteria API to order a query result based on a property of a Java class for which I am querying:

[url] User user = new User();
Session session = HibernateUtil.beginTransaction();
Criteria criteria = session.createCriteria(User.class);
Order order = Order.asc("loginName");
criteria.addOrder(order);
List results = criteria.list();
HibernateUtil.commitTransaction();
for (int i = 0; i<results.size(); i++) {
System.out.println(results.get(i).toString());
}
[/url]

Here's a little tutorial on using the Critieria API that you might find usefuL:



Quote:
A common requirement for any application is to have the results that are generated sorted by a particular column or attribute. Again, the Criteria API makes sorting your results extremely easy by providing you with an addOrder method in the
Critieriaclass.

The addOrder method takes something called an Order object as an argument, which itself is a pretty straight forward component.

An Order object is created by using one of the two static methods in the Order class, namely the static asc(String) and desc(String) methods. Basically, you just chose whether you want your list to be sorted in ascending order, or descending order, and pass to the appropriate method the name of the field on which you want to sort. So, to sort all of my User's based on their loginName, in a descending order, I would create an Order object like so:

Order order = Order.asc("loginName");

Then, you just pass the order object to the Criteria object's addOrder method, and your results will come back ordered in exactly the manner that you have specified.


Check out the JavaDoc for the Order:

http://www.hibernate.org/hib_docs/v3/api/org/hibernate/criterion/Order.htmlhttp://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=09howtousethecriteriaapi

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.