JPA实体扩展类包含@Id [英] JPA entity extends class contain @Id
本文介绍了JPA实体扩展类包含@Id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有实体类都包含id作为主键,我可以创建包含所有公共字段的抽象类,并允许所有类扩展此类如下:
i have entities classes all contains id as primary key, can i create abstract class which contains all common fields and allow all classes extends this class as the following :
public abstract class CommonFields{
@Id
@Column(name = "ID")
private long id;
public void setId(long id) {
this.id = id;
}
public long getId() {
return id;
}
}
@Entity
@Table
public class B extends CommonFields{
String carModel;
}
@Entity
@Table
public class A extends CommonFields{
String name;
}
谢谢大家
推荐答案
您可以使用@MappedSupperclass的公共字段注释类
You can annotate the class with the common fields with @MappedSupperclass
@MappedSuperclass
public abstract class CommonFields{
@Id
@Column(name = "ID")
private long id;
public void setId(long id) {
this.id = id;
}
public long getId() {
return id;
}
}
来自@MappedSuperclass doc:
From the @MappedSuperclass doc:
指定一个类,其映射信息应用于从中继承的
实体。映射的超类没有为其定义单独的
表。
Designates a class whose mapping information is applied to the entities that inherit from it. A mapped superclass has no separate table defined for it.
这篇关于JPA实体扩展类包含@Id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文