晚上好。我正在尝试使用 JPA/Hibernate 将类层次结构映射到单个表,并在我的子类上收到一条错误,指出“实体没有定义主键属性”。类定义如下:
@Entity
@Table(name = "payments")
@Inheritance (strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="creditDebitFlag",discriminatorType=DiscriminatorType.STRING)
@Veto
public abstract class Payment implements IPayment, Serializable{
private static final long serialVersionUID = 8354755060201271169L;
public Integer id;
...
@Override
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
public void setId(Integer id) {
this.id = id;
}
@Override
public Integer getId() {
return this.id;
}
}
@Entity
@DiscriminatorValue("C")
@Veto
public class CreditPayment extends Payment implements Serializable {
private static final long serialVersionUID = 1L;
public CreditPayment() {
super();
}
}
我做错了什么?我已通读 Hibernate docs我花了相当多的时间在谷歌上搜索,但看不到我错过了什么。
预先感谢您的帮助!
最佳答案
您的问题与继承无关。出现此错误是因为您在 setter 中具有持久性映射注释。这些注释应该位于字段或 getter 中。在这种情况下,只需将注释从 setId 移动到 getId(如果您有其他 setter 的这些注释,也将它们移动)。
JPA 规范:
When property-based access is used, the object/relational mapping annotations for the entity class annotate the getter property accessors[7]....
[7] These annotations must not be applied to the setter methods.
关于Hibernate Single_Table 持久化 实体没有在子类上定义主键属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7883777/