org.hibernate.WrongClassException: Object with id: 592 was not of the specified subclass: pojo.Product (Discriminator: ChildProduct)

Hibernate JIRA | Nicolas Bouillon | 5 years ago
tip
Your exception is missing from the Samebug knowledge base.
Here are the best solutions we found on the Internet.
Click on the to mark the helpful solution and get rewards for you help.
  1. 0

    Given an abstract parent class : {noformat} <class abstract="true" name="pojo.Product" table="Product"> <id column="ID" name="id"> <generator class="native" /> </id> <discriminator column="type" type="java.lang.String" force="true" /> [...] {noformat} and one (or more) subclasses, defined in a separate file : {noformat} <subclass discriminator-value="Childroduct" extends="pojo.Product" lazy="true" name="pojo.plugin.ChildProduct"> <join fetch="join" table="ChildProduct"> <key column="productID" /> [...] {noformat} The separate file can be loaded or not in the hibernate configuration, with a plugin system. That's why force=true was placed on the discriminator element. Given this class that have a relationship with the parent class : {noformat} <class name="pojo.ProductCategory"> <id column="ID" name="id"> <generator class="native"/> </id> <property name="code"/> <property name="description"/> <bag name="products" table="product_productcategory"> <key column="productCategoryID"/> <many-to-many class="pojo.Product" column="productID" /> </bag> </class> {noformat} The following code throws an exeception : {code} ProductCategory pc = productCategoryDao.findById(1); for (Product product: pc.getProducts()) { System.out.println(product.getId()); } {code} {noformat} Caught: org.hibernate.WrongClassException: Object with id: 592 was not of the specified subclass: pojo.Product (Discriminator: ChildProduct) org.hibernate.WrongClassException: Object with id: 592 was not of the specified subclass: pojo.Product (Discriminator: ChildProduct) at org.hibernate.loader.Loader.getInstanceClass(Loader.java:1626) at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1460) at org.hibernate.loader.Loader.getRow(Loader.java:1384) at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:640) at org.hibernate.loader.Loader.doQuery(Loader.java:856) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) at org.hibernate.loader.Loader.loadCollection(Loader.java:2175) at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:61) at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:622) at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:82) at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1606) at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:379) at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:112) at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:266) {noformat} The generated SQL to load the Product collection doesn't contains a WHERE clause to limit the result to known discriminators values.

    Hibernate JIRA | 5 years ago | Nicolas Bouillon
    org.hibernate.WrongClassException: Object with id: 592 was not of the specified subclass: pojo.Product (Discriminator: ChildProduct)
  2. 0

    Given an abstract parent class : {noformat} <class abstract="true" name="pojo.Product" table="Product"> <id column="ID" name="id"> <generator class="native" /> </id> <discriminator column="type" type="java.lang.String" force="true" /> [...] {noformat} and one (or more) subclasses, defined in a separate file : {noformat} <subclass discriminator-value="Childroduct" extends="pojo.Product" lazy="true" name="pojo.plugin.ChildProduct"> <join fetch="join" table="ChildProduct"> <key column="productID" /> [...] {noformat} The separate file can be loaded or not in the hibernate configuration, with a plugin system. That's why force=true was placed on the discriminator element. Given this class that have a relationship with the parent class : {noformat} <class name="pojo.ProductCategory"> <id column="ID" name="id"> <generator class="native"/> </id> <property name="code"/> <property name="description"/> <bag name="products" table="product_productcategory"> <key column="productCategoryID"/> <many-to-many class="pojo.Product" column="productID" /> </bag> </class> {noformat} The following code throws an exeception : {code} ProductCategory pc = productCategoryDao.findById(1); for (Product product: pc.getProducts()) { System.out.println(product.getId()); } {code} {noformat} Caught: org.hibernate.WrongClassException: Object with id: 592 was not of the specified subclass: pojo.Product (Discriminator: ChildProduct) org.hibernate.WrongClassException: Object with id: 592 was not of the specified subclass: pojo.Product (Discriminator: ChildProduct) at org.hibernate.loader.Loader.getInstanceClass(Loader.java:1626) at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1460) at org.hibernate.loader.Loader.getRow(Loader.java:1384) at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:640) at org.hibernate.loader.Loader.doQuery(Loader.java:856) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) at org.hibernate.loader.Loader.loadCollection(Loader.java:2175) at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:61) at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:622) at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:82) at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1606) at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:379) at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:112) at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:266) {noformat} The generated SQL to load the Product collection doesn't contains a WHERE clause to limit the result to known discriminators values.

    Hibernate JIRA | 5 years ago | Nicolas Bouillon
    org.hibernate.WrongClassException: Object with id: 592 was not of the specified subclass: pojo.Product (Discriminator: ChildProduct)

    Root Cause Analysis

    1. org.hibernate.WrongClassException

      Object with id: 592 was not of the specified subclass: pojo.Product (Discriminator: ChildProduct)

      at org.hibernate.loader.Loader.getInstanceClass()
    2. Hibernate
      PersistentBag.iterator
      1. org.hibernate.loader.Loader.getInstanceClass(Loader.java:1626)
      2. org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1460)
      3. org.hibernate.loader.Loader.getRow(Loader.java:1384)
      4. org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:640)
      5. org.hibernate.loader.Loader.doQuery(Loader.java:856)
      6. org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
      7. org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
      8. org.hibernate.loader.Loader.loadCollection(Loader.java:2175)
      9. org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:61)
      10. org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:622)
      11. org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:82)
      12. org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1606)
      13. org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:379)
      14. org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:112)
      15. org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:266)
      15 frames