Question

Have a @OneToMany relationship in Parent class mapped with orphan-removal="true" and Cascade.ALL.

First, I persist the parent object.

Then I clone the parent at the ManagedBean, like this:

Parent newParent = (Parent) org.apache.commons.lang.SerializationUtils.clone(this.object);

, so the user can make some changes and click Save button.

At this time I persist newParent but get the following error:

##"Caused by: org.hibernate.HibernateException: Don't change the reference to a collection with cascade="all-delete-orphan":package.Parent.children"##

I really can't understand why I get this error since the relationship is not required. When I first persist the parent, I didn't set anything to this relation and it works ok.

Looks like the problem occurs after cloning, but I can't tell why!

Can someone please help me with this one?

Thanks.

Was it helpful?

Solution

Hibernate gave you some references when you loaded the parent, you can not persist the parent again if you dont show up with the orphan-removal collection references hibernate gave you, so whatever you're planning to do with the data, the only thing you can do with those collections is to add or remove items, but always keeping the references.

If you lose any reference for some extraordinary reason, load the parent again and use the new references hibernate gives you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top