0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HibernateのWrongClassException

Posted at

ORMにhibernateを利用しているとき
DBからデータを取得した際org.hibernate.WrongClassException が出た場合は、データが不整合になっているケースが高い。

java.lang.Object
  extended byjava.lang.Throwable
      extended byjava.lang.Exception
          extended byjava.lang.RuntimeException
              extended byorg.hibernate.exception.NestableRuntimeException
                  extended byorg.hibernate.HibernateException
                      extended byorg.hibernate.WrongClassException

public class WrongClassException extends HibernateException

Thrown when Session.load() selects a row with the given primary key (identifier value) but the row's discriminator value specifies a subclass that is not assignable to the class requested by the user.

Session.load()メソッドで、主キーで行を取得しようとした時に、行のdiscriminator(識別子)の値が期待されているものと違うサブクラスを表している場合に投げられる。
http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch10.html#inheritance-tablepersubclass-discriminator

discriminatorは1つのテーブルにクラスの階層構造をストアするときに使うもの(table-per-classヒエラルキー)

The element is required for polymorphic persistence using the table-per-class-hierarchy mapping strategy and declares a discriminator column of the table. The discriminator column contains marker values that tell the persistence layer what subclass to instantiate for a particular row.

たとえば

org.hibernate.WrongClassException: Object [id=1] was not of the specified subclass [myEntity] : loaded object was of wrong class class [myAnotherEntity]

この場合ObjectのIDはmyEntityがサブクラスなのでこれが取得されるのを期待してるんだけど取得しようとしてるのが別のmyAnotherEntityになってしまっているよという意味になり
直接DBなどいじったりして外部参照してるIDが違うところについてたりとかそういう不整合が起きている可能性がある

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?