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

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

问题描述

您将如何配置标注在下面的例子中code?我想坚持只用JPA注解,避免Hibernate的具体依赖性。 下面是正确的code?

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等多个版本,一个数据库表每类。有些人可能会增加额外的字段(列),但大多数人会简单地利用基本字段从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:

既抽象又具体的类可以是实体。 既抽象又具体类可以用Entity注解,映射为实体进行注释,并询问了为实体。

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(+休眠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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