简体   繁体   中英

JPA Inheritence IdClass

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM