I have a property that is a collection.
I mapped it so as to order it by the listOrder index, like:
Code:
<set name="links" inverse="true" order-by="list_order">
<key>
<column name="category_id" />
</key>
<one-to-many class="com.thalasoft.learnintouch.core.domain.Link" />
</set>
The full Hibernate mapping is:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 18, 2010 1:03:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.thalasoft.learnintouch.core.domain.LinkCategory" table="link_category" dynamic-insert="true" dynamic-update="true">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<version name="version" type="int">
<column name="version" not-null="true" />
</version>
<property name="name" type="string">
<column name="name" length="50" not-null="true" />
</property>
<property name="description" type="string">
<column name="description" not-null="false" />
</property>
<set name="links" inverse="true" order-by="list_order">
<key>
<column name="category_id" />
</key>
<one-to-many class="com.thalasoft.learnintouch.core.domain.Link" />
</set>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 18, 2010 1:03:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.thalasoft.learnintouch.core.domain.Link" table="link" dynamic-insert="true" dynamic-update="true">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<version name="version" type="int">
<column name="version" not-null="true" />
</version>
<property name="name" type="string">
<column name="name" length="50" not-null="true" />
</property>
<property name="description" type="string">
<column name="description" />
</property>
<property name="image" type="string">
<column name="image" length="50" />
</property>
<property name="url" type="string">
<column name="url" />
</property>
<property name="listOrder" type="int">
<column name="list_order" not-null="true" />
</property>
<many-to-one name="linkCategory" class="com.thalasoft.learnintouch.core.domain.LinkCategory" fetch="select">
<column name="category_id" />
</many-to-one>
</class>
</hibernate-mapping>
I coded a test to make sure the property is retrieved in a correct order, that is, is ordered by the listOrder index:
Code:
@Test
public void testCollection() {
link0 = new Link();
link0.setName("Thalasoft");
link0.setImage("image0.png");
link0.setListOrder(3);
linkCategory0.addLink(link0);
link1 = new Link();
link1.setName("Libe");
link1.setImage("image1.jpg");
link1.setListOrder(2);
linkCategory0.addLink(link1);
link2 = new Link();
link2.setName("Google");
link2.setImage("image2.gif");
link2.setListOrder(1);
linkCategory0.addLink(link2);
LinkCategory linkCategory = linkCategoryDao.findById(linkCategory0.getId(), false);
assertEquals(3, linkCategory.getLinks().size());
assertEquals(link2.getName(), linkCategory.getLinks().iterator().next().getName());
assertEquals(link1.getName(), linkCategory.getLinks().iterator().next().getName());
assertEquals(link0.getName(), linkCategory.getLinks().iterator().next().getName());
}
But when running the test it retrieves the collection in a random order, different every build, ignoring the listOrder index:
Quote:
org.junit.ComparisonFailure: expected:<[Google]> but was:<[Thalasoft]>
If lucky, sometimes the test succeeds, only to fail on the next build.
Of course, I need the database itself to order the collection items.
I read the part on sorted collections at
http://docs.jboss.org/hibernate/core/3. ... tions.html specifically:
"If you want the database itself to order the collection elements, use the order-by attribute of set, bag or map mappings.".
But could not see the solution.
Any clue ?