Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Hibernate: getting a sorted loadAll() result...

  1. #1
    Join Date
    Aug 2005
    Location
    Canada
    Posts
    14

    Default Hibernate: getting a sorted loadAll() result...

    Since I've been told to use HibernateTemplate.loadAll() in my Dao, is there a way to specify the sort order of the returned list?

    When dealing with mapped collections, it's easy to specify the order-by attribute in the mapping so that the sorting is done on the DB.

    When dealing with with loadAll() however, I get whatever order the DB has them in. Is there a mapping setting to push the sorting onto the DB when using loadAll(), or do I have to resort to find("form Object o order by o.name")?

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    loadAll uses the criteria to load the entities without doing any ordering. You have to alternatives: 1. you can sort the set afterwards , 2. extend the template or use the callback and add the ordering criteria.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    May 2006
    Posts
    6

    Default

    I'm trying to use Hibernate callback using a named query.
    Query query = session.getNamedQuery("find.all.users");

    Could you pls tell me how do I provide the ordering info through the above approach.

    Thanks
    spagilla

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    See the hibernate reference documentation - you can either modify your named query to include an "order by" or if you will always need an order set, modify the mapping so that the returned collection is sorted at retrieval time.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  5. #5
    Join Date
    May 2006
    Posts
    6

    Default

    Thanks for the reply.

    I'm displaying a result list on a page which has multiple column names in the list.

    private List findAllUsersTest(final String queryName, final int startRecord, final int endRecord) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
    public Object doInHibernate(Session session) throws HibernateException, SQLException {
    Query query = session.getNamedQuery(queryName);
    query.setFirstResult(startRecord);
    query.setMaxResults(endRecord);
    List list = query.list();
    return list;
    }
    });
    }


    The user can sort the list on the page by clicking the column of his choice.
    I dont want to write 10 named queries (5 for asc and 5 for desc) if I have 5 columns in the list.
    So I'm using a fixed named query.
    Is there a way that I could sort the returned collection by providing the column that I need to sort on?

  6. #6
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    named queries are static - if you want to be able to select the column and the ordering direction then externalize these from your query so you can set it up when executing the query.
    Basically, write the query as if it's not named and add the asc/desc as well as the column programaticaly and then simply externalize the query but keep the attribute binding inside your code. See Hibernate reference documentation for more info.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  7. #7

    Default loadAll with criteria(order) example

    Quote Originally Posted by Costin Leau View Post
    loadAll uses the criteria to load the entities without doing any ordering. You have to alternatives: 1. you can sort the set afterwards , 2. extend the template or use the callback and add the ordering criteria.
    Hi Costin

    Don't you have an example lying around.

    I am trying to sort following:
    return getHibernateTemplate().loadAll(ConversionRuleset.c lass);

    I wan't to sort the ConversionRuleset's by letting Hibernate do the sort using the callback and adding an ordering criteria.

    But how do i do that ?

    Anyone have a sample lying around ?

    Best Regards...
    Cemil
    Last edited by Cemil; Sep 28th, 2006 at 10:09 AM.

  8. #8
    Join Date
    Nov 2005
    Posts
    148

    Default Sorting

    If u want to sort ur models, Firstly u have to implement "Comaprable" Interface,which is the part of "java.lang" package.And then IF u getting collection from the DAO.then write the one line:

    if u r using JDK 1.5
    Collections.sort(urDAORetaunredList);
    This will sort the "urDAORetaunredList" alphabeticaly in case of String.




    Model code
    Code:
    
    import java.util.Date;
    import javax.persistence.*;
    
    
    
    @Entity
    @Table(name = "routes")
    public class RoutesModel extends AbstractBaseModel implements Comparable<RoutesModel> {
    	private String pattern;
    	private String comment;
    	
    	public RoutesModel(){
    	}
    	public void setPattern(String pattern){
    		this.pattern = pattern;
    	}
    	@Column(name = "pattern" )
    	public String getPattern(){
    		return this.pattern;
    	}
    	public void setComment(String comment){
    		this.comment = comment;
    	}
    	@Column(name = "comment" )
    	public String getComment(){
    		return this.comment;
    	}
    	
    	public int compareTo(RoutesModel comparedObject) {
    		// TODO Auto-generated method stub
    		return this.getComment().compareToIgnoreCase(comparedObject.getComment());
    	}
    }
    Regards,
    Shahzad Ahsan
    Ooober Brain
    ooober.com

  9. #9

    Default

    Hi shahzad992

    Thanks a lot.

    Appreciate youre help.

    It's working now.

    Best Regards

    Cemil Özdemir

  10. #10
    Join Date
    Oct 2006
    Posts
    1

    Thumbs up

    Hi shahzad992,
    I have done something similar before using xdoclet tags for hibernate. when retrieving a collection, it works fine and is ordered properly according to my requirement. But when I try to save a collection then it gives a class cast exception.

    This is my doclet mapping in Abc,

    * @hibernate.set table="ABC_DEF" cascade="none" "sort="natural"
    * batch-size="10"
    * @hibernate.collection-key column="ABC_ID"
    * @hibernate.collection-many-to-many class="com.xx.xxx.Def"
    * column="DEF_ID"

    Do you have any explanation regarding this situation?

    Regards,
    Asiri

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •