facebook

Sort Order in Hibernate

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #261587 Reply

    Erik Costlow
    Member

    I’ve used MyEclipse to reverse engineed hibernate mappings for my database. When I walk through the Set of objects that are gathered from the foreign key relation, they return in a different and unpredictable order each time. How do I sort the items so that they show up in the same order every time?

    
    <hibernate-mapping>
        <class name="AdminProfile" table="admin_profile" catalog="enterprise">
            <id name="adminId" type="java.lang.Integer">
                <column name="admin_id" />
                <generator class="increment" />
            </id>
    ...
            <set name="divisionAdmins" inverse="true">
                <key>
                    <column name="admin_id" not-null="true" />
                </key>
                <one-to-many class="DivisionAdmin" />
            </set>
    </hibernate-mapping>
    
    
    <hibernate-mapping>
        <class name="DivisionAdmin" table="division_admin" catalog="enterprise">
            <composite-id name="id" class="DivisionAdminId">
                <key-many-to-one name="adminProfile" class="AdminProfile">
                    <column name="admin_id" />
                </key-many-to-one>
                <key-many-to-one name="division" class="Division">
                    <column name="division_id" />
                </key-many-to-one>
            </composite-id>
        </class>
    </hibernate-mapping>
    
    <!-- The primary key for the above DivisionAdmin contains an AdminId and DivisionId, referencing each table -->
    
    <hibernate-mapping>
        <class name="Division" table="division" catalog="enterprise">
            <id name="divisionId" type="java.lang.Integer">
                <column name="division_id" />
                <generator class="increment" />
            </id>
    ...
            <set name="divisionAdmins" inverse="true">
                <key>
                    <column name="division_id" not-null="true" />
                </key>
                <one-to-many class="DivisionAdmin" />
            </set>
        </class>
    </hibernate-mapping>
    

    When I did the Hibernate reverse-engineering, each object contains a set of the objects.

    AdminProfile
    Set divisionAdmins

    DivisionAdmin
    Division division

    Basically, I want to be able to iterate through these and for a given AdminProfile, list all Divisions in the right order.

    
    <c:forEach items="${admin.divisionAdmins }" var="divisionAdmin">
        <c:set var="division" value="${divisionAdmin.id.division }" />
        <li>${division.name } is id # ${division.divisionId}</li>
    </c:forEach>
    

    How do I control the sorting order?

    #261589 Reply

    Haris Peco
    Member

    costlow,

    You have to use order-by attribute in set element, for example :

    <set name=”divisionAdmins” inverse=”true” order-by=”your_properties_fromDivisionAdmin asc|desc”>
    <key>
    <column name=”division_id” not-null=”true” />
    </key>
    <one-to-many class=”DivisionAdmin” />
    </set>

    MyEclipse doesn’t support order-by in Reverse Engineering and you have to edit your mapping file

    Regards,

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: Sort Order in Hibernate

You must be logged in to post in the forum log in