Java 使用 JPA (+Hibernate) 继承抽象类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3827494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inherited abstract class with JPA (+Hibernate)
提问by Manius
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?
您将如何在以下示例代码中配置注释?我只想坚持使用 JPA 注释并避免 Hibernate 特定的依赖项。下面的代码正确吗?
@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. 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?
最终,我想使用 Hibernate 的 SchemaExport 类从中生成模式创建脚本。在上面的情况下,这两个类应该只导致创建一个名为“RefExample”的表,其中包含来自“RefData”的三列。这会起作用吗?
采纳答案by Arthur Ronald
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.
抽象类和具体类都可以是实体。抽象类和具体类都可以使用 Entity 注释进行注释,映射为实体,并作为实体进行查询。
实体可以扩展非实体类,非实体类可以扩展实体类。
As you want a single table, you should use Single Tableinheritance.
由于您需要单个表,因此应该使用Single Table继承。
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 purposeof such a mapped superclass is to define state and mapping information that is common to multiple entity classes.
实体可以从提供持久实体状态和映射信息的超类继承,但它本身不是实体。通常,这种映射超类的目的是定义多个实体类共有的状态和映射信息。
Keep in mind you can notuse @Entity and @MappedSuperclass at the same time.
请记住,您不能同时使用 @Entity 和 @MappedSuperclass。
回答by lalit
回答by Amirtharaj
@MappedSuperclassis worked for me. I was struggling to map a view to 2 objects which are Parent and child classes. My view is joined from 2 tables. Primary keys from both the tables are present in the view. @DiscriminatorColumnis 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.
@MappedSuperclass对我有用。我正在努力将视图映射到 2 个对象,它们是父类和子类。我的视图是从 2 个表连接的。两个表的主键都存在于视图中。@DiscriminatorColumn对我不起作用,因为它需要一个专门分配给对象数据类型的列,并且它正在抛出'repeated Column in object exception'
我无法解决的问题。
I read this forum and I tried @MappedSuperclassannotation. It does the trick.
我阅读了这个论坛,并尝试了@MappedSuperclass注释。它可以解决问题。
I've put @MappedSuperclassat the superclass and put the @Idand @GeneratedValuein the superclass identifier. In the sublass I've given as
我已将@MappedSuperclass放在超类中,并将@Id和@GeneratedValue放在超类标识符中。在我给出的子类中
@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 @DiscriminatorColumnworked for me.
在不使用@DiscriminatorColumn 的情况下继承连接表的休眠注释对我有用。