A Party has a set of Emails:
Code:
<hibernate-mapping>
<class name="model.common.Party" table="PARTY" schema="REBAR" lazy="false">
<id name="partyId" type="java.lang.Long">
<column name="PARTY_ID" precision="12" scale="0" />
<generator class="sequence">
<param name="sequence">PARTY_ID_SEQ</param>
</generator>
</id>
<set name="emails" cascade="all-delete-orphan" lazy="false" inverse="true">
<key>
<column name="PARTY_ID" precision="12" scale="0" not-null="true" />
</key>
<one-to-many class="model.common.Email" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="model.common.Email" table="EMAIL" schema="REBAR">
<id name="emailId" type="java.lang.Long">
<column name="EMAIL_ID" precision="12" scale="0" />
<generator class="sequence">
<param name="sequence">EMAIL_ID_SEQ</param>
</generator>
</id>
<many-to-one name="party" class="model.common.Party" fetch="select">
<column name="PARTY_ID" precision="12" scale="0" />
</many-to-one>
<property name="current" type="true_false">
<column name="IS_CURRENT" length="1" />
</property>
<property name="primary" type="true_false">
<column name="IS_PRIMARY" length="1" />
</property>
<property name="usageType" type="string">
<column name="USAGE_TYPE" length="18" />
</property>
<property name="emailAddress" type="string">
<column name="EMAIL_ADDRESS" length="200" />
</property>
</class>
</hibernate-mapping>
Since it is detached and never has a party stored in the email, before I update (or merge), I set the party back into the email:
Code:
if (party.getEmails() != null) {
for (Email email : party.getEmails()) {
email.setParty(party);
}
}
In order to update I use Spring where this = HibernateDaoSupport:
Code:
this.getHibernateTemplate().update(party);
I also tried this, but it still didn't remove orphans. Only merge() did:
Code:
this.getSession.update(party);