2009-05-28 243 views
2

考慮以下JPA實體。我的應用程序實例類必須始終具有4個特殊的Envelope實例的OneToOne引用,但它也有一組0-infinite用戶定義的信封。這甚至有可能嗎?單向和/或雙向引用可能嗎?JPA實體映射爲OneToOne以及OneToMany

@Entity(name = "Application_Instance") 
public class ApplicationInstance implements Serializable { 

    @Id 
    private int databaseId; 
    private Envelope accountTransfersEnvelope = new Envelope("Account Transfers"); 
    @OneToOne 
    private Envelope newTransationsEnvelope = new Envelope("New Transactions"); 
    @OneToOne 
    private Envelope incomeEnvelope = new Envelope("Income Envelope"); 
    @OneToOne 
    private Envelope creditCarEnvelope= new Envelope("Credit Card"); 
    @OneToMany 
    protected Set<Envelope> userEnvelopes = new HashSet<Envelope>(); 

//rest of class 
} 

回答

2

你可以使用一個連接表映射這樣做:

@OneToMany 
@JoinTable(name = "USER_ENVELOPE", 
      joinColumns = { @JoinColumn(name = "APP_ID") }, 
      inverseJoinColumns { @JoinColumn(name = "ENVELOP_ID") })   
protected Set<Envelope> userEnvelopes = new HashSet<Envelope>();