org.hibernate.WrongClassException

There are no available Samebug tips for this exception. Do you have an idea how to solve this issue? A short tip would help users who saw this issue last week.

  • 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.
    via by Nicolas Bouillon,
  • 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.
    via by Nicolas Bouillon,
    • org.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: org.openxava.invoicing.model.CommercialDocument (Discriminator: 0) at org.hibernate.loader.Loader.getInstanceClass(Loader.java:1477) at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1308) at org.hibernate.loader.Loader.getRow(Loader.java:1230) at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603) at org.hibernate.loader.Loader.doQuery(Loader.java:724) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) at org.hibernate.loader.Loader.loadEntity(Loader.java:1881) at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71) at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65) at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072) at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434) at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415) at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165) at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223) at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126) at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905) at org.hibernate.impl.SessionImpl.get(SessionImpl.java:842) at org.hibernate.impl.SessionImpl.get(SessionImpl.java:835) at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:182) at org.openxava.model.impl.JPAPersistenceProvider.find(JPAPersistenceProvider.java:26) at org.openxava.model.impl.POJOPersistenceProviderBase.find(POJOPersistenceProviderBase.java:89) at org.openxava.model.impl.POJOPersistenceProviderBase.find(POJOPersistenceProviderBase.java:60) at org.openxava.model.impl.MapFacadeBean.findEntity(MapFacadeBean.java:1424) at org.openxava.model.impl.MapFacadeBean.findEntity(MapFacadeBean.java:1555) at org.openxava.model.impl.MapFacadeBean.findEntity(MapFacadeBean.java:213) at org.openxava.model.MapFacade.findEntity(MapFacade.java:526) at org.openxava.tab.impl.EntityTabDataProvider.getEntity(EntityTabDataProvider.java:173) at org.openxava.tab.impl.EntityTabDataProvider.doCalculations(EntityTabDataProvider.java:125) at org.openxava.tab.impl.EntityTabDataProvider.nextChunk(EntityTabDataProvider.java:62) at org.openxava.tab.impl.EntityTab.nextChunk(EntityTab.java:298) at org.openxava.tab.impl.TableModelBean.getRow(TableModelBean.java:120) at org.openxava.tab.impl.TableModelBean.getValueAt(TableModelBean.java:171) at org.openxava.tab.impl.XTableModelDecoratorBase.getValueAt(XTableModelDecoratorBase.java:63) at org.openxava.tab.impl.HiddenXTableModel.getValueAt(HiddenXTableModel.java:70) at org.openxava.tab.Tab.createTableModel(Tab.java:271) at org.openxava.tab.Tab.getTableModel(Tab.java:250)

    Users with the same issue

    j.kienzlej.kienzle
    1 times, last one,