我有一个使用 Hibernate 进行数据持久性的应用程序,上面有 Spring(为了很好的衡量标准)。直到最近,应用程序中还有一个持久类 A:
@Entity
public class A {
@Id
@Column(unique = true, nullable = false, updatable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
public String name;
}
我已经添加了 A 的一个子类,称为 B:
@Entity
public class B extends A {
public String description;
}
添加B后,我现在无法加载A。引发了以下异常:
class org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException :: Object with id: 1 was not of the specified subclass: A (Discriminator: null); nested exception is org.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: A (Discriminator: null)
我在B中添加了如下注解和属性,似乎解决了问题。这是解决问题的正确方法吗?
...
@DiscriminatorFormula("(CASE WHEN dtype IS NULL THEN 'A' ELSE dtype END)")
public class A {
private String dtype = this.getClass().getSimpleName();
...