在我的项目中,我有一个名为 BaseEntity 的 POJO,如下所示。
class BaseEntity{
private int id;
public void setId(int id){
this.id=id;
}
public int getId(){
return id;
}
}
以及一组其他 POJO 实体类,如 Movie、Actor、...
class Movie extends BaseEntity{
private String name;
private int year;
private int durationMins;
//getters and setters
}
我使用 BaseEntity 只是为了在某些界面中将其用作占位符。我永远不必存储 BaseEntity 对象。我必须只存储从 BaseEntity 扩展的实体对象。我应该如何注释这些类,以便从 BaseEntity 扩展的每个实体获得一个表。对于电影,它应该是(id、name、year、durationMins)。
最佳答案
我在一个完全不相关的帖子中找到了答案。我只需要将 BaseEntity 注释为@MappedSuperclass。下面的代码完成了我所需要的。
@MappedSuperclass
class BaseEntity {
@Id
private int id;
//getters and setters.
}
@Entity
class Movie extends BaseEntity {
@Column
private String name;
@Column
private int year;
@Column
private int durationMins;
//getters and setters
}
关于使用基本实体的 hibernate 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18764296/