Wednesday, December 16, 2009

Hibernate Intricacies

Forgive me please if the following post is not 100% clear.

Imagine this: You've got a Proposal object with a List<Resource> field. Each Resource has a Map<ResourceAttribute> field. You're using Hibernate, and the Resource.hbm.xml contains a <map> element with cascade="all-delete-orphan". If you call Session.save(resource) for a particular resource belonging to a proposal, and later call Session.update(proposal) for that proposal, you will get the following exception - Don't change the reference to a collection with cascade="all-delete-orphan".

Obviously you haven't directly changed the resource's reference to its map of attributes, but, somehow, by saving the resource directly, instead of jumping up a level and saving the proposal to which it belongs, you're causing Hibernate to consider the reference changed.

No comments: