tags:

views:

1316

answers:

1

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?

+1  A: 

Everything looks right. My advice is to use @MappedSuperclass on top of @EmbeddedId:

@MappedSuperclass
public abstract class ModelBaseRelationship implements Serializable {

@Embeddable
public static class Id implements Serializable {

    public Long entityId1;
    public Long entityId2;

    @Column(name = "ENTITY1_ID")
    public Long getEntityId1() {
        return entityId1;
    }

    @Column(name = "ENTITY2_ID")
    public Long getEntityId2() {
        return entityId2;
    }

    public Id() {
    }

    public Id(Long entityId1, Long entityId2) {
        this.entityId1 = entityId1;
        this.entityId2 = entityId2;
    }

   }

   protected Id id = new Id();

   @EmbeddedId
   public Id getId() {
        return id;
   }

   protected void setId(Id theId) {
        id = theId;
   }

 }

I omitted obvious constructors/setters for readability. Then you can define UserContract as

@Entity
@AttributeOverrides( {
        @AttributeOverride(name = "entityId1", column = @Column(name = "user_id")),
        @AttributeOverride(name = "entityId2", column = @Column(name = "contract_id"))
})
public class UserContract extends ModelBaseRelationship {

That way you can share primary key implementation for other many-to-many join entities like UserContract.

grigory