Hi
We have several many-to-many relationships, but with additional data on the mapping table. For whatever (historical) reasons, our many-to-many relationships are mapped as 2 one-to-may like this:
Code:
<hibernate-mapping>
<class name="net.umbrella.data.model.ParticipantPriceitemMapModel" table="PARTICIPANT_PRICEITEM_MAP">
<composite-id name="id" class="net.umbrella.data.model.ParticipantPriceitemMapIdModel">
<key-property name="participantid" type="int">
<column name="PARTICIPANTID" />
</key-property>
<key-property name="priceitemid" type="int">
<column name="PRICEITEMID" />
</key-property>
</composite-id>
<many-to-one name="participants" class="net.umbrella.data.model.ParticipantModel" update="false" insert="false" fetch="select" cascade="persist,save-update">
<column name="PARTICIPANTID" not-null="true" />
</many-to-one>
<many-to-one name="priceitems" class="net.umbrella.data.model.PriceitemModel" update="false" insert="false" fetch="select" cascade="persist,save-update">
<column name="PRICEITEMID" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
You can see that this Map-Table contains references to a PARTICIPANT and to a PRICEITEM. I want to delete the PARTICIPANT, but leave the PRICEITEM intact.
Without cascading, Hibernate will tell me that the deleted ParticipantPriceitemMapModel will be re-saved. I guess that's because the PRICEITEM still holds a valid reference to the entity in its getParticipantPriceitemMaps() collection.
So I added an Interceptor which "cleans" up map entries before deleting them:
Code:
public void onDelete(Object entity, Serializable id,
Object[] state, String[] propertyNames, Type[] types){
if ( entity instanceof ParticipantPriceitemMapModel) {
ParticipantPriceitemMapModel map = (ParticipantPriceitemMapModel) entity;
map.getParticipants().getParticipantPriceitemMaps().remove(entity);
map.getPriceitems().getParticipantPriceitemMaps().remove(entity);
}
}
Now I get a java.util.ConcurrentModificationException in cascadeCollection. I guess I'm removing stuff whie cascading, not a good idea? But then, how do I do this?
Thanks
Simon