hiddenuser
hiddenuser

Reputation: 525

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

Upvotes: 0

Views: 1453

Answers (1)

cmd
cmd

Reputation: 11841

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

Upvotes: 1

Related Questions