我有以下情况需要解决,但无法使其工作(尝试过 Hibernate 和 EclipseLink):
Table_1:
Column_A is Primary Key
... some other columns do follow
.
Table_2:
Column_x is Primary Key and is Foreign Key to Table_1.Column_A
Column_y is Primary Key and is Foreign Key to Table_1.Column_A
Column_z is Primary Key
因此,表 2 有一个复合主键。
我尝试通过以下方式实现它:
class Table_1 {
@Id int Column_A;
}
.
class Table_2 {
@EmbeddedId PK key;
@Embeddable class PK {
@OneToOne(targetEntity=Table_1.class)
@JoinColumn(name="Column_x",referencedColumnName="Column_A")
int Column_x;
@OneToOne(targetEntity=Table_1.class)
@JoinColumn(name="Column_y",referencedColumnName="Column_A")
int Column_y;
int Column_z;
public boolean equals(Object O) { ... }
public int hashCode() { ... }
}
}
然而,当我运行时,我从 EclipseLink 得到提示,在 @Embeddable 中我只能使用“基本”注释。因此,我的问题是如何解决上面绘制的场景?
我无权访问类 Table_1 的源代码,但必须按原样使用它。此外,很可能有更多的类/表建立到 Table_1 的外键。
使用@IdClass。
See,
http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#JPA_2.0 http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#JPA_2.0
@IdClass(PK.class)
class Table_2 {
@Id
@OneToOne(targetEntity=Table_1.class)
@JoinColumn(name="Column_x",referencedColumnName="Column_A")
Table_1 Column_x;
@Id
@OneToOne(targetEntity=Table_1.class)
@JoinColumn(name="Column_y",referencedColumnName="Column_A")
Table_1 Column_y;
@Id
int Column_z;
public boolean equals(Object O) { ... }
public int hashCode() { ... }
}
}
class PK {
int Column_x;
int Column_y;
int Column_z;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)