【发布时间】:2010-05-21 14:41:49
【问题描述】:
我正在为两个不同的表创建相同的实体。为了对两个实体做不同的表映射等,但只有一个地方的其余代码 - 一个抽象超类。最好的办法是能够在超类中注释通用的东西,例如列名(因为它们将是相同的),但这不起作用,因为 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 吗?
谢谢, 克里斯
【问题讨论】:
标签: java inheritance jpa annotations