JPA继承IdClass [英] JPA Inheritence IdClass

查看:183
本文介绍了JPA继承IdClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在多重继承实体复合键的情况下使用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;
    }

IdClassEntity2应该是什么:

what should be 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通过继承传播.

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

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

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 {
    ...

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

这篇关于JPA继承IdClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆