gpt4 book ai didi

jpa - JPA:可嵌入类中的外键

转载 作者:行者123 更新时间:2023-12-04 14:41:38 32 4
gpt4 key购买 nike

我有以下情况要解决,但无法正常工作(尝试了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。

看到,
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;
}

关于jpa - JPA:可嵌入类中的外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10281511/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com