标题听起来很蹩脚和混淆,但问题出在这里:
我有一个名为 Price 的实体,它有两个名为" status "和" approvalStatus "。这些元素分别来自 Status 和 ApprovalStatus 类。后两者都扩展了 Domain 类,它映射了一个多域表。
在我的jUnit中,当我调用 Status 列表或 ApprovalStatus 列表时,hibernate带来了没问题。但是当我打电话给我的Price实体时,我遇到了以下问题:
javax.persistence.PersistenceException: org.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: net.x.ApprovalStatus (loaded object was of wrong class class net.x.Status)] with root cause
org.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: net.x.ApprovalStatus (loaded object was of wrong class class net.x.Status)
作为测试,我删除了" 状态"我的价格实体中的元素。它工作了!当我有多个类继承自同一个实体的元素时,会弹出这个问题。
这是我的课程(手动剥离,太大了:/)
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="CD_DOMAIN_GROUP", discriminatorType=DiscriminatorType.INTEGER)
@Table(name="DOMAIN")
@Cacheable
public abstract class Domain {
@Id
@Column(name="CD_DOMAIN")
private Integer code;
public Domain () {
}
public Domain (Integer code) {
this.code = code;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code= code;
}
}
@Entity
@DiscriminatorValue("1")
@Cacheable
public class Status extends Domain implements Serializable {
private static final long serialVersionUID = 1L;
public static Integer ACTIVE= 1;
public static Integer INATIVE = 2;
public Status() {
}
public Status(Integer code) {
super(code);
}
}
@Entity
@DiscriminatorValue("2")
@Cacheable
public class ApprovalStatus extends Domain implements Serializable {
private static final long serialVersionUID = 1L;
public static Integer APPROVED = 1;
public static Integer REJECTED = 2;
public static Integer PENDING = 3;
public ApprovalStatus () {
}
public ApprovalStatus (Integer code) {
super(code);
}
}
@Entity
@Table(name="PRICE")
public class Price {
@Id
@Column(name="CD_PRICE")
private Integer code;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ID_STATUS", referencedColumnName="CD_DOMAIN")
private Status status;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ID_APPROVAL_STATUS", referencedColumnName="CD_DOMAIN")
private ApprovalStatus approvalStatus;
}
有谁知道发生了什么事?我错过了一些配置吗?我的地图有点不对劲吗?
我正在使用hibernate 4.2.13.Final。