データの永続化に Hibernate を使用し、Spring を (適切な測定のために) 使用するアプリケーションがあります。最近まで、アプリケーションには 1 つの永続クラス A がありました。
@Entity
public class A {
@Id
@Column(unique = true, nullable = false, updatable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
public String name;
}
それ以来、B と呼ばれる A のサブクラスを追加しました。
@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();
...