我正在为两个不同的表创建相同的实体。为了对两个实体做不同的表映射等,但只有一个地方的其余代码 - 一个抽象父类(super class)。最好的办法是能够在父类(super class)中注释通用的东西,例如列名(因为它们将是相同的),但这不起作用,因为 JPA 注释不被子类继承。这是一个例子:
public abstract class MyAbstractEntity {
@Column(name="PROPERTY") //This will not be inherited and is therefore useless here
protected String property;
public String getProperty() {
return this.property;
}
//setters, hashCode, equals etc. methods
}
我想继承并只指定子特定的东西,比如注释:
@Entity
@Table(name="MY_ENTITY_TABLE")
public class MyEntity extends MyAbstractEntity {
//This will not work since this field does not override the super class field, thus the setters and getters break.
@Column(name="PROPERTY")
protected String property;
}
有什么想法或者我必须在子类中创建字段、getter 和 setter 吗?
谢谢, 克里斯
最佳答案
您可能希望使用 @MappedSuperclass
类注释 MyAbstractEntity,以便 hibernate 将 MyAbstractEntity 的配置导入子级中,您不必覆盖该字段,只需使用父级的。该注释是 hibernate 的信号,它也必须检查父类。否则它假定它可以忽略它。
关于java - 是否可以将 JPA 注释添加到父类(super class)实例变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2883033/