A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity
Let's Play "Who Owns That Collection?" With Hibernate If you have used Hibernate and mapped a one-to-many relationship you've probably come across the "delete orphan" feature. This feature offers cascade delete of objects orphaned by code like the following: Preference pref = getSomePreference(); user.getPreferences().remove(pref); In the above code, a specific // Do not do this! public void setPreferences(Set newPreferences) { this.preferences = newPreferences == null ? new HashSet<Preference>() : newPreferences; } Code like the above results in a A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance What is happening here is that Hibernate requires complete ownership of the user.setPreferences(new HashSet<Preference>()); So the point is that Hibernate's delete orphan abstraction is leaking into your domain model object. This is pretty much unavoidable but is a leaky abstraction nonetheless that developers need to be aware of lest they run into the error mentioned above. So how can you avoid this problem? The only sure way that I know of is to make the setter method private, since passing any new collection or null results in the "owning entity" error. This way only Hibernate will use the setter method to load up user objects (it invokes the method reflectively after setting it accessible via the Reflection API). Then you could add a method public void addPreference(Preference p) { getPreferences().add(p); p.setUser(this); } This has the nice side effect of establishing the bi-directional relationship between user and preference, assuming your model allows bi-directional navigation. You could also add a null check if you are paranoid. Removing a preference from a user is equally simple. You can write a helper method user.getPreferences().remove(aPref); Essentially, you can operate on the collection returned by user.getPreferences().clear(); Note that all the above discussion refers to one-to-many relationships that specify the delete orphan cascade type; usually you will specify both "all" and "delete-orphan." In cases where you are only using the "all" cascade option, the semantics are quite different. Assuming the normal case where the "many" side of the relationship owns the relationship -- i.e. you used // Assume only using cascade="all" and an inverse="true" mapping in User.hbm.xml user.getPreferences().remove(aPref); // does not cause database delete session.delete(aPref); // this causes the database deletion One last thing to note in the above is that you must remove |