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).
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.
For example, I want to create a User and its contracts and persist them in a easy way.
Currently what I have is this: In User.java:
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private List<UserContract> userContract;
In Contract.java:
@OneToMany(mappedBy = "contract", fetch = FetchType.LAZY)
private Collection<UserContract> userContract;
And my UserContract.java:
@Entity
public class UserContract {
@EmbeddedId
private UserContractPK userContractPK;
@ManyToOne(optional = false)
private User user;
@ManyToOne(optional = false)
private Contract contract;
And my UserContractPK:
@Embeddable
public class UserContractPK implements Serializable {
@Column(nullable = false)
private long idContract;
@Column(nullable = false)
private String email;
Is this the best way to achieve my goals?