title: How can I insert a record in join table of a many to many hibernate annotation?
body: <p>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</p>
  
  <p>I have to insert record in tbluserdomainrelation from both side (that is from tbluser and tbldomain)</p>
  
  <p>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</p>
  
  <p>In <code>user.class</code> I have written</p>
  
  <pre><code>@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&lt;VirtualDomain&gt; getVirtualdomainset() {
          return virtualdomainset;
  }
  
  public void setVirtualdomainset(Set&lt;VirtualDomain&gt; virtualdomainset) {
          this.virtualdomainset = virtualdomainset;
  }
  </code></pre>
  
  <p>While in domain table I have entry</p>
  
  <pre><code>@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE},fetch=FetchType.EAGER, mappedBy = "virtualdomainset", targetEntity = User.class)
  
  public Set&lt;User&gt; getUserset() {
          return userset;
  }
  
  public void setUserset(Set&lt;User&gt; userset) {
          this.userset = userset;
  }
  </code></pre>