2009-05-28 237 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>();