Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I insert a record in join table of a many to many hibernate annotation?

Tags:

hibernate

I have many to many relationship between two table and generate new join table in hibernate now how can I insert a record in join table by updatation of both table say two table are tbluser and tbldomain

I have to insert record in tbluserdomainrelation from both side (that is from tbluser and tbldomain)

At present I can insert record in join table only when I save or update tbluser but I want record to be inserted when I update relationship in domain table also

In user.class I have written

@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 domain table I have 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;
}
like image 891
lakhaman Avatar asked May 09 '09 09:05

lakhaman


People also ask

What should be done for many-to-many joins in hibernate?

In order to map a many-to-many association, we use the @ManyToMany, @JoinTable and @JoinColumn annotations. Let's have a closer look at them. The @ManyToMany annotation is used in both classes to create the many-to-many relationship between the entities.

What is the use of @JoinTable annotation?

Annotation Type JoinTable. Specifies the mapping of associations. It is applied to the owning side of an association. A join table is typically used in the mapping of many-to-many and unidirectional one-to-many associations.


1 Answers

I suspect you may be falling at the first hurdle:

As has been previously stated, Hibernate will update the object that you call update() on and then cascade down to any subsequent objects that require updating, following the rules you specify in the @Cascade attribute.

The problem is that your User class owns the relationship and adding a User object to the VirtualDomain.userset collection does not alter the User object in any way. Thus even with a cascade, Hibernate will not update the User object as it doesn't think that it has to.

Instead, when you add an object to the VirtualDomain.userset collection, ensure that the VirtualDomain is added to the User.virtualdomainset collection as well.

// 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 cases like this I find it useful to provide a helper method for adding objects to collections, rather than using direct access to the collections themselves. e.g.

public class VirtualDomain {

    Set userset;

   /* snip... */

    public void addUser( User user ) {
        getUserset().add( user );
        user.getVirtualdomainset().add( this );
    }
}

Although it's worth remembering the advice of the Hibernate fore-fathers:

"In a real system, you may not have a many-to-many association. Our experience is that there is almost always other information that must be attached to each link... the best way... is via an intermediate assocation class." (Java Persistence With Hibernate, p. 297/298 , ISBN 1-932394-88-5)

like image 115
Luke Avatar answered Jan 02 '23 23:01

Luke