问题描述
在我的项目中,我有一个名为BaseEntity的POJO,如下所示。
class BaseEntity {
private int id ;
public void setId(int id){
this.id = id;
}
public int getId(){
return id;
$ b 还有一组POJO实体类,如Movie,演员,...
class Movie扩展BaseEntity {
私有字符串名称;
private int年;
private int durationMins;
// getters和setters
}
我只使用BaseEntity将其用作某些界面中的占位符。我从来不需要存储BaseEntity对象。我只需要存储从BaseEntity扩展的实体对象。我应该如何注释这些类,以便从BaseEntity扩展的每个实体获得一个表。对于电影来说,它应该像(id,name,year,durationMins)。
解决方案我在完全无关的帖子中找到了答案。我只需要将BaseEntity注释为@MappedSuperclass。
@MappedSuperclass $ b $ class BaseEntity {
@Id
private int id;
//获得者和设置者。
}
@Entity
类Movie扩展BaseEntity {
@Column
私有字符串名称;
@Column
private int year;
@Column
private int durationMins;
// getters and setters
}
In my project I have a POJO called BaseEntity as shown below.
class BaseEntity{
private int id;
public void setId(int id){
this.id=id;
}
public int getId(){
return id;
}
}
And a set of other POJO entity classes like Movie, Actor,...
class Movie extends BaseEntity{
private String name;
private int year;
private int durationMins;
//getters and setters
}
I'm using BaseEntity only for using it as a place holder in some interfaces. I never have to store a BaseEntity object. I have to store only the entity objects extended from BaseEntity. How should I annotate these classes so that I get one table per entity extended from the BaseEntity. For movie it should be like (id, name, year, durationMins).
解决方案 I found the answer in a totally unrelated post. I just have to annotate BaseEntity as @MappedSuperclass. The following code done what I needed.
@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 Annotation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!