title: Bidirectional mapping of entities in Hibernate and spring mvc
body: <p>I am trying to map two entities User(userid,name,password,address) and Role(roleId,roleName).
  I am trying to do bidirectional OneToMany mapping between User and Role.</p>
  
  <p>My User entity:</p>
  
  <pre><code>@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
  @JoinTable(
          name="UserRole",
          joinColumns = @JoinColumn(name="USER_ID"),
          inverseJoinColumns = @JoinColumn(name="ROLE_ID")
  )
  public Set&lt;Role&gt; role;
  </code></pre>
  
  <p>My Role entity:</p>
  
  <pre><code>@ManyToOne(cascade = CascadeType.ALL)
  @JoinTable(
          name="UserRole",
          joinColumns= @JoinColumn(name="ROLE_ID")
  )
  private User user;
  </code></pre>
  
  <p>Now the corresponding intermediate table created in the database has following attributes.</p>
  
  <p>userrole-> attributes( user_userId,ROLE_ID,USER_ID )</p>
  
  <p>Now when i add set of items to a user. ROLE_ID and USER_ID of userrole table gets populated but user_userId remain null. What is the purpose of user_useerId.Should i manully make it primary key in the table user_userId?</p>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>i did the following editing in Role entity</p>
  
  <pre><code>@ManyToOne(cascade = CascadeType.ALL)
  @JoinTable(
          name="UserRole",
          joinColumns = @JoinColumn(name = "ROLE_ID"),
          inverseJoinColumns = @JoinColumn(name="USER_ID")
  )
  private User user;
  </code></pre>
  
  <p>And now when i checked the table in the database, the "userrole" table is perfectly ok and contains only (USER_ID,ROLE_ID)</p>
  
  <p>I want to know why shouldn't i map two entities though this way?</p>