- This topic has 1 reply, 2 voices, and was last updated 18 years, 5 months ago by
Haris Peco.
-
AuthorPosts
-
Erik CostlowMemberI’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 divisionAdminsDivisionAdmin
Division divisionBasically, 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?
Haris PecoMembercostlow,
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,
-
AuthorPosts