I have a Parent class with a HashMap of Child classes. Child has a parent attribute to refer to its Parent.
I have successfully persisted the Map and both classes with Hibernate 2.1.3 to MySQL. I have had a good deal of trouble getting the classes to load from the DB - the parent attribute of Child is not set. It is set before saving when it is added to the Parent. The objects are working, I'm just having trouble persisting and retrieving them.
I have tried a couple of things, and have only been able to get it to work by changing the Java code in the setMap method of Parent to iterate through the map and assign parent values to each Child. I'm assuming, though, that there must be a better way to do this. A way that doesn't rely on this iteration.
I'll post the mapping for the two classes that came closest without having to iterate through the map. When done like this, a parent id is stored by the child, but it's always the same number - and never an actual primary key of any parent. I've no idea where the number comes from, though it's always the same.
Many thanks for any help or suggestions.
mapping for the Parent (Party):
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="parties.Party" table="party">
<id name="id" type="string" column="id">
<generator class="assigned"/>
</id>
<discriminator column="type"/>
<!-- timestamp allows updating with assigned id -->
<timestamp name="lastSave" column="timestamp" unsaved-value="null"/>
<map
name="childMap"
table="child_map"
cascade="all-delete-orphan">
<key column="owner_id"/>
<index-many-to-many column="key_id" class="parties.ChildType"/>
<many-to-many column="element_id" class="parties.Child"/>
</map>
<subclass name="parties.Person"
discriminator-value="person">
</subclass>
<subclass name=.parties.Organization"
discriminator-value="organization">
</subclass>
</class>
</hibernate-mapping>
mapping for child:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="parties.Child"
table="child">
<id name="id" type="string" column="id">
<generator class="uuid.hex"/>
</id>
<many-to-one name="party"
column="party_id" not-null="true"/>
</class>
</hibernate-mapping>
[/code]