JPA Inheritence IdClass

会有一股神秘感。 提交于 2019-12-12 04:37:41

问题


How to use IdClass in multiple inheritance entity composite key case ?

    @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;
    }

what should be IdClassEntity2 :

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

or

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

回答1:


You can use a @MappedSuperClass or @Entity to declare the @IdClass. The pk is propagated via inheritance.

For example, using @MappedSuperClass, you can do the following:

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

 @Entity
 public class Entity2 extends Entity1 {
    ...

Using @Entity, follow the same paradigm



来源:https://stackoverflow.com/questions/21844459/jpa-inheritence-idclass

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!