JPA实体映射为OneToOne以及一对多
-
06-09-2019 - |
题
考虑以下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>();
不隶属于 StackOverflow