Question

hey i want to do nested sorting in hibernate.

i have a set of applications in course object. application has a time and type attribute. I want to sort by type first and then within application of particular type i want to sort it by time.

can i use it as follows??

order-by="type desc,time asc" 

Note that type should be sorted descendingly ( type is a int datatype in java) and time ascendingly (time is of date datatype in jave )

eg of ordering
Type time
5 1
5 2
5 3
4 1
4 2

time was given as integer in this example

Was it helpful?

Solution

You can sort just like you said. You may also want to add the comparable interface to your objects so you can sort them in your services.

The following hibernate file has an example of using an sql bit for sorting

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="au.gov.abs.maserati.domain.entity.CURF" 
        table="CURF" >
        <id name="ID" column="ID">
            <generator class="native" />
        </id>

        <property name="title" />
        <property name="description" />
        <property name="active"
            type="yes_no" 
            not-null="true" />

        <set name="specialConditions" cascade="none"
            order-by="sortOrder, Text1"
            where=" Discriminator in ( 'SPECIAL-CURF-AGREEMENT' ) and active = 'Y' ">
            <key column="parentID" not-null="false" />
            <one-to-many
                class="au.gov.abs.maserati.domain.entity.Condition" />
        </set>
    </class>
</hibernate-mapping>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top