JPA实体映射为OneToOne以及OneToMany

时间:2009-05-28 05:20:09

标签: hibernate jpa entity one-to-one

考虑以下JPA实体。我的应用程序实例类必须始终具有对4个特殊Envelope实例的OneToOne引用,但它也有一组0-无限用户定义的包络。这甚至可能吗?是否可以同时使用单向和/或双向参考?

    @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
}

1 个答案:

答案 0 :(得分:2)

您可以使用连接表映射执行此操作:

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