How can I insert an entry into the connection table from many, many sleep mode annotations?

I have many different relations between the two tables and creating a new connection table in sleep mode now how can I insert an entry into the connection table by updating both tables say two tables: tbluser and tbldomain

I need to insert an entry in tbluserdomainrelation on both sides (i.e. from tbluser and tbldomain)

Currently, I can only insert an entry into the connection table when saving or updating tbluser, but I want an entry that will be inserted when I update relations in the domain table as well

In user.class I wrote

 @ManyToMany(targetEntity = VirtualDomain.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE},fetch=FetchType.EAGER) @JoinTable(name = "tblUserDomainRel", joinColumns = @JoinColumn(name = "userid"), inverseJoinColumns = @JoinColumn(name = "domainid")) public Set<VirtualDomain> getVirtualdomainset() { return virtualdomainset; } public void setVirtualdomainset(Set<VirtualDomain> virtualdomainset) { this.virtualdomainset = virtualdomainset; } 

While in the domain table I have an entry

 @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE},fetch=FetchType.EAGER, mappedBy = "virtualdomainset", targetEntity = User.class) public Set<User> getUserset() { return userset; } public void setUserset(Set<User> userset) { this.userset = userset; } 
+4
source share
3 answers

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.

 // given a VirtualDomain object VirtualDomain domain = getSession().load( VirtualDomain.class, 1234 ); // and a User object User user = getSession().load( User.class, 5678 ); // add the User to the VirtualDomain domain.getUserset().add( user ); // but also add the VirtualDomain to the user user.getVirtualdomainset().add( domain ); // update the VirtualDomain and the Cascade settings will also update the User getSession().update( domain ); 

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; /* snip... */ 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)

+9
source

What do your classes look like?

I assume that you have a Domain class and a User class. In the Domain class, suppose you have a Users collection. Add the User object to the Users collection and call SaveOrUpdate on the Domain object. Also make sure that you specify the correct cascading option (for example, cascading = "save-update".

This is a little big picture. We need more detailed information about what is not working.

0
source

You have created a custom class as the owner of the relationship. If you want to go the other way, you need to switch their display so that Intenaint has @JoinTable. Or you must manually force it using @PrePersist or @PreUpdate on Domaintable to force an update for the user that inserts a row into the connection table. I am sure that there are other ways to achieve it, but these are the ones that come to mind.

 @PreUpdate example : @EntityListeners( Domaintable.class ) @Entity public class Domaintable { User user; .... @PreUpdate public void onPreUpdate( DomainTable domainTable ) { if( domainTable.getUser() != null ) { entitymanager.update( domainTable.getUser() ); } } .... } 
0
source

All Articles