I have the following mappings:
Code:
<hibernate-mapping>
<class name="some.package.ApplicationEvidence" table="application_evidence" >
<composite-id name="pk" class="some.package.ApplicationEvidencePK">
<key-many-to-one name="applicationValidation" class="some.package.ApplicationValidation">
<column name="ava_id" />
</key-many-to-one>
<key-many-to-one name="customerEvidence" class="some.package.CustomerEvidence">
<column name="cev_id" />
</key-many-to-one>
</composite-id>
<!-- Some properties -->
</class>
<class name="some.package.ApplicationValidation" table="application_validations">
<id name="id" type="java.lang.Long" column="id">
<generator class="sequence">
<param name="sequence">ava_seq</param>
</generator>
</id>
<!-- other properties ... -->
<set name="applicationEvidence" lazy="true" inverse="true" cascade="all">
<key>
<column name="ava_id" />
</key>
<one-to-many class="some.package.ApplicationEvidence" />
</set>
</class>
<class name="some.package.CustomerEvidence" table="customer_evidence">
<id name="id" type="java.lang.Long" column="id">
<generator class="sequence">
<param name="sequence">cev_seq</param>
</generator>
</id>
<set name="applicationEvidence" lazy="true" inverse="true" cascade="all">
<key>
<column name="cev_id" />
</key>
<one-to-many
class="some.package.ApplicationEvidence"
/>
</set>
</class>
</hibernate-mapping>
Table ApplicationEvidence has a composite primary key involving two columns, both of which are foreign keys to ApplicationValidation and CustomerEvidence respectively.
In my java code I create both a CustomerEvidence and ApplicationValidation object, and then create the ApplicationEvidencePK using the two objects (without first saving anything).
i.e.
Code:
CustomerEvidence custEv = new CustomerEvidence(...., new HashSet(),...);
ApplicationValidation appVal = new ApplicationValidation(....,new HashSet()...);
ApplicationEvidencePK pk = new ApplicationEvidencePK(custEv,appVal);
ApplicationEvidence evid = new ApplicationEvidence(pk);
custEv.getApplicationEvidence().add(evid);
appVal.getApplicationEvidence().add(evid);
....
...
//save the application
When I save, the CustomerEvidence and ApplicationValidation objects are stored correctly but the ApplicationEvidence object is not inserted and I get an update statement on my console output instead of an insert.
Incidentally, while mucking about trying to get this to work, i added the
unsaved-value="any" to my composite-id tag, and I then recieved an integrity constraint error, hibernate was finally trying to insert the ApplicationEvidence object, but did so before the CustomerEvidence object.
Can anyone shed some light on this?