考虑以下JPA实体。我的应用程序实例类必须始终有一个OneToOne参考信封的4种特殊情况,但它也有一组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
}
有帮助吗?

解决方案

您可以用一个连接表映射做到这一点:

@OneToMany
@JoinTable( name = "USER_ENVELOPE",
            joinColumns = { @JoinColumn( name = "APP_ID" ) },
            inverseJoinColumns { @JoinColumn( name = "ENVELOP_ID" ) } )        
protected Set<Envelope> userEnvelopes = new HashSet<Envelope>();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top