I have classes A, B and C (each with their own table). A has a many-to-many B kept in association table AB. Each of those has a many-to-many to C kept in association table AB_C. Because the AB relationship is related to more information that just which A is related to which B, it also must maintain relationships to C, I created an AB class that is also mapped to the AB table (based on section 6.3.2 in Hibernate in Action). The mappings and objects are as follows:
A mapping
Code:
<hibernate-mapping>
<class name="A" table="A">
... some other properties, including an id, A_ID
<idbag name="myBs" table="AB" cascade="save-update" lazy="false" fetch="join">
<collection-id type="java.lang.Integer" column="AB_ID">
<generator class="sequence">
<param name="sequence">MY_SEQ</param>
</generator>
</collection-id>
<key column="A_ID" not-null="true"/>
<composite-element class="AB">
<parent name="myA"/>
<many-to-one name="myB"
column="B_ID"
class="B"
not-null="true"
cascade="save-update"/>
</composite-element>
</idbag>
</class>
</hibernate-mapping>
A Java ObjectCode:
public class A
{
private Integer A_ID;
private List<AB> myBs;
public addB(B b)
{
AB tempReltn = new AB(this, B);
a2bRelationships.add(tempReltn);
}
}
B mappingCode:
<hibernate-mapping>
<class name="B" table="B">
... some other properties, including an id, B_ID
</class>
</hibernate-mapping>
B Java ObjectCode:
public class B
{
private Integer B_ID;
...other properties
}
AB mappingCode:
<hibernate-mapping>
<class name="AB" table="AB">
... some other properties, including an id, AB_ID
... no inverse relationship, is one needed?
<idbag name="myCs" table="AB_C" cascade="save-update" lazy="false" fetch="join">
<collection-id type="java.lang.Integer" column="ABC_ID">
<generator class="sequence">
<param name="sequence">MY_SEQ</param>
</generator>
</collection-id>
<key column="AB_ID" not-null="true"/>
<composite-element class="ABC">
<parent name="myAB"/>
<many-to-one name="myC"
column="C_ID"
class="C"
not-null="true"
cascade="save-update"/>
</composite-element>
</idbag>
</class>
</hibernate-mapping>
AB Java ObjectCode:
public class AB
{
private Integer AB_ID;
//parent in the mapping
private A myA;
//Composite element with many to one reltn
private B myB;
private List<ABC> myCs;
public addC(C b)
{
ABC tempReltn = new ABC(this, C);
ab2cRelationships.add(tempReltn);
}
}
C Java ObjectCode:
public class C
{
private Integer C_ID;
...other properties
}
ABC stuff is mapped pretty much identically to the AB stuff, only using an AB as an object in the relationship.
When I debug this stuff, it seems as though all the objects are there. The A has the correct list of AB's which holds the correct B's and the AB's have the needed ABC objects with the correct C's. But when I save the A, the AB's and B's are persisted and the tables are updated correctly. However, the C's are not saved and therefore neither are the corresponding relationships. The sql to do so is never executed. I noticed during debug that the A's ArrayList of AB's is a PersistedIdentifierBag, but the AB's ArrayList of ABC's is not, its just an ArrayList.
Open to any suggestions, thanks!