Problem: mapping many-to-one via link table
need a mapping file ( Child.hbm.xml ) which uses a link collection table (LINKER) to map setOther & getOther to retrieve a single value or null from the OTHER table.
Attempts:
Almost got it working by by creating "map" and modifying the class to return a List, however in the real world I have no control over the definition of the classes or tables.
Context:
1. using normalized table-per-subclass mapping strategy
2. The setter/getter works with a single object (Other) not a Collection
3. There is a link collection table that which defines the relations (LINKER).
4. Using hibernate mapping file per class
Code:
class Parent
{
private String _id;
public String getID() { return _id; }
public void setID( String id ) { _id = id; }
}
class Child extends Parent
{
private Other _other;
public Other getOther() { return _other; }
public void setOther( Other other ) { _other = other; }
}
class Other
{
private String _id;
public String getID() { return _id; }
public void setID( String id ) { _id = id; }
}
Code:
table PARENT ( pid, ...)
table CHILD ( cid, ... )
table OTHER ( oid, ...)
table LINKER( cid, oid );
Parent.htm.xml
Code:
<hibernate-mapping package="example">
<class name="Parent" table="PARENT">
<id column="pid" name="ID" type="string" unsaved-value="null">
<generator class="uuid.hex"/>
</id>
</class>
</hibernate-mapping>
Child.hbm.xml ** THIS IS WHERE I NEED HELP
Code:
<hibernate-mapping>
<joined-subclass extends="Parent" name="Child" table="CHILD">
<key column="cid"/>
<bag cascade="all" name="Other" table="LINKER">
<key>
<column name="cid" not-null="true"/>
</key>
<many-to-many class="Other">
<column name="oid" not-null="true"/>
</many-to-many>
</bag>
</joined-subclass>
</hibernate-mapping>
Other.htm.xml
Code:
<hibernate-mapping>
<class name="Other" table="OTHER">
<id column="oid" name="ID" type="string" unsaved-value="null">
<generator class="uuid.hex"/>
</id>
</class>
</hibernate-mapping>