I suspect you might run into the first hurdle:
As stated earlier, Hibernate will update the object that you call update() , and then cascade to any subsequent objects that require updating, following the rules specified in the @Cascade attribute.
The problem is that your User class is related and adding a User object to the VirtualDomain.userset collection does not modify the User object in any way. Thus, even with the help of the cascade, Hibernate will not update the User object, since it does not consider what it should.
Instead, when you add an object to the VirtualDomain.userset collection, make sure that VirtualDomain added to the User.virtualdomainset collection.
In such cases, I find it useful to use a helper method to add objects to collections, rather than directly accessing the collections themselves. eg.
public class VirtualDomain { Set userset; public void addUser( User user ) { getUserset().add( user ); user.getVirtualdomainset().add( this ); } }
Although it is worth recalling the advice of sleeping fathers:
“In a real system, you may not have many-to-many associations. Our experience is that there is almost always other information that needs to be attached to each link ... the best way ... through an intermediate class of associations” . (Java Persistence With Hibernate, pp. 297/298, ISBN 1-932394-88-5)
source share