简体   繁体   English

使用JPA(+ Hibernate)继承抽象类

[英]Inherited abstract class with 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. 我只想坚持使用JPA注释,并避免使用Hibernate特定的依赖项。 Is the code below correct? 以下代码是否正确?

@Entity
public class RefExample extends 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.) (这些类将有多个版本,RefSomeOtherExample等,每个类有一个db表。有些可能会添加其他字段(列),但大多数只会使用从“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;
    }
}

Ultimately I'd like to generate schema creation scripts from this using Hibernate's SchemaExport class. 最后,我想使用Hibernate的SchemaExport类从中生成模式创建脚本。 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". 在上面的情况下,这两个类应该只导致创建一个名为“RefExample”的表,其中包含“RefData”中的三列。 Will this work? 这会有用吗?

From JPA 1.0 specification: 从JPA 1.0规范:

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. 抽象和具体类都可以使用实体注释进行注释 ,映射为实体,并作为实体进行查询。

Entities can extend non-entity classes and non-entity classes can extend entity classes . 实体可以扩展非实体类,非实体类可以扩展实体类

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 {

But if you do not want to rely on JPA inheritance strategies, you can use MappedSuperclass instead: 但是,如果您不想依赖JPA继承策略,则可以使用MappedSuperclass:

@MappedSuperclass
public abstract class RefData {

JPA specification 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 . 通常,这种映射的超类的目的 是定义多个实体类共有的状态和映射信息

Keep in mind you can not use @Entity and @MappedSuperclass at the same time. 请记住, 您不能同时使用@Entity和@MappedSuperclass。

@MappedSuperclass is worked for me. @MappedSuperclass对我有用 I was struggling to map a view to 2 objects which are Parent and child classes. 我正在努力将视图映射到2个父类和子类的对象。 My view is joined from 2 tables. 我的观点来自2个表格。 Primary keys from both the tables are present in the view. 两个表中的主键都存在于视图中。 @DiscriminatorColumn is not worked for me since it requires a column exclusively allotted to data type of the object and also it is throwing 'repeated Column in object exception' which I could not solve. @DiscriminatorColumn对我来说不起作用,因为它需要一个专门分配给对象数据类型的'repeated Column in object exception'并且它抛出'repeated Column in object exception' ,这是我无法解决的。

I read this forum and I tried @MappedSuperclass annotation. 我读了这个论坛,我尝试了@MappedSuperclass注释。 It does the trick. 它成功了。

I've put @MappedSuperclass at the superclass and put the @Id and @GeneratedValue in the superclass identifier. 我把@MappedSuperclass放在超类中,并将@Id@GeneratedValue放在超类标识符中。 In the sublass I've given as 在我给出的子类中

@Entity
@Table(name="view_name")

and used sub class object to fetch the data from view. 并使用子类对象从视图中获取数据。 That's it. 而已。

Inheritance in hibernate annotations for Joined table with out using @DiscriminatorColumn worked for me. 使用@DiscriminatorColumn对已加入表的hibernate注释进行继承为我工作。

You can use one of the inheritance strategy to to this. 您可以使用其中一种继承策略来实现此目的。 Your case looks to be the case of Single class for the hierarchy. 您的案例看起来是层次结构的Single类的情况。

Check this 检查一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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