[英]How do you join an object with a composite id on another object with a composite id but not the same composite id? (JPA annotations)
僅使用FOO_ID,如何將條形對象列表映射到Foo對象?
@Entity
@Table(name="FOO")
class Foo {
@EmbeddedId
private FooPK primaryKey;
@OneToMany
@JoinColumn(name="FOO_ID", referencedColumnName="FOO_ID") //does not work
private List<Bar> bars;
}
@Embeddable
class FooPK {
@Column(name="FOO_ID")
private String id;
@Column(name="FOO_SUB_ID")
private String subId;
}
@Entity
@Table(name="BAR")
class Bar {
@EmbeddedId
private BarPK primaryKey;
//other attributes and methods
}
@Embeddable
class BarPK {
@Column(name="FOO_ID")
private String id;
@Column(name="BAR_ID")
private String barId;
@Column(name="BAR_SUB_ID")
private String barSubId;
}
我試過了:@JoinColumn(如上)和@JoinTable在另一個包含FOO_ID作為主鍵的表上。 都不行。
按照此處所述嘗試: http : //stackoverflow.com/questions/5305687/join-entity-with-composite-key
Foo
沒有任何特殊的映射:
class Foo {
@EmbeddedId
private FooPK primaryKey;
@OneToMany(mappedBy="foo")
private List<Bar> bars;
}
因此,將其映射到另一側:
class Bar {
@EmbeddedId
private BarPK primaryKey;
@ManyToOne
@MapsId("id")
private Foo foo;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.