aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate Collections and Ordering Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Collections and Ordering" Watch "Hibernate Collections and Ordering" New topic
Author

Hibernate Collections and Ordering

lavnish lalchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 79
Hi
I am a little confused on Collections , Ordering and Hibernate
Consider scenario we have a Student class and Book class, and Student has an ordered collection of Books

Now we can have following scenarios

store an ORDERED collection from java to DB table
  • - if order in java is b1,b2,b3 then the entries in DB will be in the same order ( rownum wise ) on doing session.save(student) , right ?
  • - if order in java is b3,b2,b1 then the entries in DB will be in the same order ( rownum wise ) on doing session.save(student) , right ?

  • this will happen irrespective of value for sort="xyz"

    GET rows from DB to Java ORDERED Collection using "in memory sorting"
  • - Now here requirement is irrespective of the order of rows in DB we want a particular ordered collection in java memory we implement comparable or comparator accordingly
  • - so if <set ... sort="natural"/> in and in Book class we implement required logic in CompareTo
  • - if <set ... sort="Comparator"/> here order will be as per the logic implemented in the comparator class
  • - if <set ... sort="unsorted"> then whatever be default order in DB will come in Java collection


  • GET rows from DB to Java ORDERED Collection using "SQL sorting"
  • here the <set ... sort="unsorted"> does not play any role


  • Just wanted to check if my understanding is correct


    lavnish.blogspot.com
    Bill Gorder
    Bartender

    Joined: Mar 07, 2010
    Posts: 1666
        
        7

    store an ORDERED collection from java to DB table
    - if order in java is b1,b2,b3 then the entries in DB will be in the same order ( rownum wise ) on doing session.save(student) , right ?
    - if order in java is b3,b2,b1 then the entries in DB will be in the same order ( rownum wise ) on doing session.save(student) , right ?


    First of all the words sorted and ordered mean 2 different things when we are talking about Hibernate persistent collections. Sorted means sorted in memory using a Java comparator, while ordered is done at the database level using an SQL query with an order by clause. In this case I think you meant store a SORTED collection.

    Different collection types are wrapped by different hibernate implementations. For example for a List with no ordered annotations a bag implementation will be used and order will be ignored. Also depending on your id generation strategy the consecutive rownum assumption may be faulty as well.

    Have a look at this link
    http://docs.jboss.org/hibernate/orm/4.0/manual/en-US/html/collections.html#collections-indexed


    [How To Ask Questions][Read before you PM me]
    lavnish lalchandani
    Ranch Hand

    Joined: Feb 28, 2007
    Posts: 79
    Sorted means sorted in memory using a Java comparator, while ordered is done at the database level using an SQL query with an order by clause.

    if user decides to use rownum for both sorting in memory and ordering in the DB i guess only then they both can be same else not , right ??
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Hibernate Collections and Ordering