使用 JPA (+Hibernate) 继承抽象类 [英] Inherited abstract class with JPA (+Hibernate)

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

问题描述

您将如何在以下示例代码中配置注释?我只想坚持使用 JPA 注释并避免 Hibernate 特定的依赖项.下面的代码正确吗?

How would you configure annotations in the following example code? I'd like to stick with JPA annotations only and avoid Hibernate specific dependencies. Is the code below correct?

@Entity
public class RefExample extends RefData {

}

(这些类将有多个版本,RefSomeOtherExample 等,每个类有一个 db 表.有些可能会添加额外的字段(列),但大多数只会使用从RefData"基类继承的基本字段.)

(There will be multiple versions of these classes, RefSomeOtherExample, etc, and one db table per class. Some may add additional fields (columns) but most will simply make use of the basic fields inherited from the "RefData" base class.)

基类:

@Entity
public abstract class RefData {

    private long id;
    private String code;
    private String desc;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique = true, nullable = false)
    public long getId() {

        return id;
    }

    public void setId(long id) {

        this.id = id;
    }

    @Column(unique = true, nullable = false, length=8)
    public String getCode() {

        return code;
    }

    public void setCode(String code) {

        this.code = code;
    }

    @Column(unique = true, nullable = false, length=80)
    public String getDesc() {

        return desc;
    }

    public void setDesc(String desc) {

        this.desc = desc;
    }
}

最终,我想使用 Hibernate 的 SchemaExport 类从中生成模式创建脚本.在上面的情况下,这两个类应该只导致创建一个名为RefExample"的表,其中包含来自RefData"的三列.这行得通吗?

Ultimately I'd like to generate schema creation scripts from this using Hibernate's SchemaExport class. In the case above these two classes should only result in the creation of a single table named "RefExample" with the three columns from "RefData". Will this work?

推荐答案

来自 JPA 1.0 规范:

From JPA 1.0 specification:

抽象类和具体类都可以是实体.抽象类和具体类都可以使用实体注释进行注释,映射为实体,并作为实体进行查询.

Both abstract and concrete classes can be entities. Both abstract and concrete classes can be annotated with the Entity annotation, mapped as entities, and queried for as entities.

实体可以扩展非实体类,非实体类可以扩展实体类.

因为你想要一个表,你应该使用单表继承.

As you want a single table, you should use Single Table inheritance.

只需定义一个鉴别器列如下:

Just define a discriminator column as follows:

@Entity
@DiscriminatorColumn(name="REF_TYPE")
public abstract class RefData {

但是如果你不想依赖 JPA 继承策略,你可以使用 MappedSuperclass 来代替:

But if you do not want to rely on JPA inheritance strategies, you can use MappedSuperclass instead:

@MappedSuperclass
public abstract class RefData {

JPA 规范

一个实体可以从一个提供持久实体状态和映射信息的超类继承,但它本身不是一个实体.通常,这种映射超类的目的是定义多个实体类共有的状态和映射信息.

An entity may inherit from a superclass that provides persistent entity state and mapping information, but which is not itself an entity. Typically, the purpose of such a mapped superclass is to define state and mapping information that is common to multiple entity classes.

记住你不能同时使用@Entity和@MappedSuperclass.

Keep in mind you can not use @Entity and @MappedSuperclass at the same time.

这篇关于使用 JPA (+Hibernate) 继承抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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