I need 3 entities: User, Contract (many to large) and middle object: UserContract (this is necessary to store some fields).
What I want to know is the correct way to define the relationships between these objects in JPA / EJB 3.0 so that operations (persist, delete, etc.) are in order.
For example, I want to create a user and his contracts and save them in a simple form.
I currently have the following: 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?
source
share