JPA继承IdClass

时间:2014-02-18 04:34:31

标签: java jpa orm jpa-2.0

如何在多继承实体复合键案例中使用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;
}

1 个答案:

答案 0 :(得分:1)

您可以使用@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,遵循相同的范例