gpt4 book ai didi

java - JPA 继承 IdClass

转载 作者:行者123 更新时间:2023-12-01 13:24:34 25 4
gpt4 key购买 nike

如何在多重继承实体组合键情况下使用IdClass?

    @IdClass(IdClassEntity1.class)
class Entity1 {
private long e1pk1;
private long e1pk2;
}
@IdClass(IdClassEntity2.class)
class Entity2 extends Entity1 {
private long e2pk1;
private long e2pk2;
}
class Entity3 extends Entity2 {
private long e3pk1;
private long e3pk2;
}

IdClassEntity2 应该是什么:

class IdClassEntity2 {
private long e1pk1;
private long e1pk2;
private long e2pk1;
private long e2pk2;
}

class IdClassEntity2 {
private IdClassEntity1 idClassEntity1;
private long e2pk1;
private long e2pk2;
}

最佳答案

您可以使用@MappedSuperClass@Entity来声明@IdClass。 pk 通过继承传播。

例如,使用@MappedSuperClass,您可以执行以下操作:

@MappedSuperClass
@IdClass(IdClassEntity1.class)
public class Entity1 {
@Id private long e1pk;
@Id private long e1pk;
...

@Entity
public class Entity2 extends Entity1 {
...

使用@Entity,遵循相同的范例

关于java - JPA 继承 IdClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21844459/

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