title: How to correctly do a manytomany join table in JPA?
body: <p>I need 3 entities: User, Contract (which are a many to many relation) and a middle entity: UserContract (this is needed to store some fields).</p>
  
  <p>What I want to know is the correct way to define the relationships between these entities in JPA/EJB 3.0 so that the operations (persist, delete, etc) are OK.</p>
  
  <p>For example, I want to create a User and its contracts and persist them in a easy way.</p>
  
  <p>Currently what I have is this:
  In User.java:</p>
  
  <pre><code>@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
      private List&lt;UserContract&gt; userContract;
  </code></pre>
  
  <p>In Contract.java:</p>
  
  <pre><code>@OneToMany(mappedBy = "contract", fetch = FetchType.LAZY)
  private Collection&lt;UserContract&gt; userContract;
  </code></pre>
  
  <p>And my UserContract.java:</p>
  
  <pre><code>@Entity
  public class UserContract {
  @EmbeddedId
  private UserContractPK userContractPK;
  
  @ManyToOne(optional = false)
  private User user;
  
  @ManyToOne(optional = false)
  private Contract contract;
  </code></pre>
  
  <p>And my UserContractPK:</p>
  
  <pre><code>@Embeddable
  public class UserContractPK implements Serializable {
  @Column(nullable = false)
  private long idContract;
  
  @Column(nullable = false)
  private String email;
  </code></pre>
  
  <p>Is this the best way to achieve my goals?</p>