My initial problem is that I want to be able to use Hibernate Tools Reverse Engineering to create SortedSets instead of Sets. After researching the issue and checking a number of forums, I realized that there is no built in functionality for this and the best option was to manually edit the *.hbm.xml files and regenerate. I was hoping that by running an ant task to modify all the <set> tags and add the attribute of "sort='natural'" and regenerating my problem would be solved. I didn't want to use and order-by attribute or comparator because I wouldn't be able to add that code with a script. My code was generated successfully with SortedSets, but I am getting a runtime exception. Any help would be greatly appreciated.
Hibernate version: 3.2.2 GA
Mapping documents:
<hibernate-mapping>
<class name="demo.EdidatTender" table="edidat_tender">
<id name="edidatTender" type="int">
<column name="edidat_tender" />
<generator class="assigned" />
</id>
<property name="company" type="string">
<column name="company" length="4" not-null="true" />
</property>
<property name="masterRefNum" type="string">
<column name="master_ref_num" length="30" not-null="true" />
</property>
<set name="edidatTenderOrders" sort="natural" inverse="true">
<key>
<column name="edidat_tender" not-null="true" />
</key>
<one-to-many class="demo.EdidatTenderOrder" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="demo.EdidatTenderOrder" table="edidat_tender_order">
<id name="edidatTenderOrder" type="int">
<column name="edidat_tender_order" />
<generator class="assigned" />
</id>
<many-to-one name="edidatTender" class="demo.EdidatTender" fetch="select">
<column name="edidat_tender" not-null="true" />
</many-to-one>
<property name="orderNumber" type="string">
<column name="order_number" length="80" not-null="true" />
</property>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Code:
EdidatTender x = (EdidatTender) session.load(EdidatTender.class, 7346616);
System.out.println(x.getMasterRefNum());
for (EdidatTenderOrder o: x.getEdidatTenderOrders()){
System.out.println("-->" + o.getEdidatTenderOrder());
}
Full stack trace of any exception that occurs:
Exception in thread "main" java.lang.ClassCastException: demo.EdidatTenderOrder
at java.util.TreeMap.compare(TreeMap.java:1093)
at java.util.TreeMap.put(TreeMap.java:465)
at java.util.TreeSet.add(TreeSet.java:210)
at java.util.AbstractCollection.addAll(AbstractCollection.java:318)
at java.util.TreeSet.addAll(TreeSet.java:258)
at org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:329)
at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:237)
at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:222)
at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:195)
at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:877)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:865)
at org.hibernate.loader.Loader.doQuery(Loader.java:729)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1994)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:163)
Name and version of the database you are using:
Informix 9