我有实体类都包含 id 作为主键,我可以创建包含所有公共(public)字段的抽象类并允许所有类扩展此类,如下所示:
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 对类的公共(public)字段进行注释
@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 文档:
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.
关于java - JPA 实体扩展类包含@Id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24177732/