I posted this before in a response to another post, but didn't get any replies, so I'm posting it again as a new post
According to Gavin,
gavin wrote:
You can't change a collection reference when the collection is mapped with cascade="all-delete-orphan".
Just make sure your getter returns the same collection instance and this exception will not occur.
That is a little easier said than done.
It is our practice to always make a copy of a collection passed in from a setter or out through a getter. I don't have TOO much of a problem doing this for the setter, since our setters for collections are private (only exist for the persistence implementation). But I have a hard time passing the same instance out of a getter that we are refering to in our class. Someone could modify the contents, I have to make sure the collection is thread safe, etc... I am curious to know why I can't change the reference, if I'm just copying the data. I can't think of a good reason why hibernate would need to refer to the same instance, but I'm sure you have one. Could someone suggest a workaround (i.e. create an immutable implementation of List and pass that to Hibernate; how would hibernate create an instance of that collection on retrieve.). Otherwise, I suppose we'll have to explictly delete orphaned objects. Thanks.