在我的项目中,我有一个名为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)。
答案 0 :(得分:27)
我在完全不相关的帖子中找到了答案。我只需要将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
}
答案 1 :(得分:5)
您可以在BaseEntity
上使用@MappedSuperClass
,并Movie
对其进行扩展。
@MappedSuperClass
class BaseEntity {
@Id
private int id;
...
}
class Movie extends BaseEntity {
...
}
答案 2 :(得分:1)
您需要的是Table Per Concrete类策略。在此策略中,您不需要为BaseEntity添加任何注释。请查看this以获取更多解释。