net.sf.hibernate.WrongClassException: Object with id: XXX was not of the specified subclass: com.woolly.store.product.DisplayProductVariation (loaded object was of wrong class)

Hibernate JIRA | Dave Birch | 1 decade ago
  1. 0
  2. 0
  3. 0

    I have 2 classes, Product (for core product info) and DisplayProduct (has extra fields) which is a subclass of Product. Display product is read only & used as a data aggregation class, i *could* load the product instance & then copy to a DisplayProduct instance, but it means 2x overhead for something desired to be an optimisation, i.e. not a great solution. What happens is the loader gets confused & after loading an instance of one class, i can not load an instance of the other, yes they are both mapped to one table, share the same key & one is a subclass, but they are not the same object in the context of the application. I get the following error: <snip> net.sf.hibernate.WrongClassException: Object with id: XXX was not of the specified subclass: com.woolly.store.product.DisplayProduct (loaded object was of wrong class) at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:531) </snip> samplecode from classes: class Product { private String code; private String description; ... //getters/setters etc ... } and; class DisplayProduct extends Product { private List images; private BigDecimal price; ... //getters/setters etc ... } If i map both of these classes to the same table; <class name="Product" table="str_products"> <id name="code" unsaved-value="null"> <generator class="assigned"/> </id> <version name="version" type="int"/> <property name="name"/> <property name="description"/> <property name="taxCode"/> ... </class> and <class name="DisplayProduct" table="str_products" mutable="false"> <id name="code" unsaved-value="null"> <generator class="assigned"/> </id> <property name="name"/> <property name="description"/> <property name="taxCode"/> ... </class> I have tried switching off the secondary level cache, but the problem seems to run deeper than this. (assuming that setting hibernate.cache.use_second_level_cache=false to do this.) Ideas?

    Hibernate JIRA | 1 decade ago | Dave Birch
    net.sf.hibernate.WrongClassException: Object with id: XXX was not of the specified subclass: com.woolly.store.product.DisplayProduct (loaded object was of wrong class)
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    I have 2 classes, Product (for core product info) and DisplayProduct (has extra fields) which is a subclass of Product. Display product is read only & used as a data aggregation class, i *could* load the product instance & then copy to a DisplayProduct instance, but it means 2x overhead for something desired to be an optimisation, i.e. not a great solution. What happens is the loader gets confused & after loading an instance of one class, i can not load an instance of the other, yes they are both mapped to one table, share the same key & one is a subclass, but they are not the same object in the context of the application. I get the following error: <snip> net.sf.hibernate.WrongClassException: Object with id: XXX was not of the specified subclass: com.woolly.store.product.DisplayProduct (loaded object was of wrong class) at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:531) </snip> samplecode from classes: class Product { private String code; private String description; ... //getters/setters etc ... } and; class DisplayProduct extends Product { private List images; private BigDecimal price; ... //getters/setters etc ... } If i map both of these classes to the same table; <class name="Product" table="str_products"> <id name="code" unsaved-value="null"> <generator class="assigned"/> </id> <version name="version" type="int"/> <property name="name"/> <property name="description"/> <property name="taxCode"/> ... </class> and <class name="DisplayProduct" table="str_products" mutable="false"> <id name="code" unsaved-value="null"> <generator class="assigned"/> </id> <property name="name"/> <property name="description"/> <property name="taxCode"/> ... </class> I have tried switching off the secondary level cache, but the problem seems to run deeper than this. (assuming that setting hibernate.cache.use_second_level_cache=false to do this.) Ideas?

    Hibernate JIRA | 1 decade ago | Dave Birch
    net.sf.hibernate.WrongClassException: Object with id: XXX was not of the specified subclass: com.woolly.store.product.DisplayProduct (loaded object was of wrong class)
  6. 0

    Hello folks, I did not find anything related to this issue on the net so I put it here: Provided are two classes A and B wich are in a 1.) many-to-many relationship 2.) with an association class Association which contains some forther attributes 2.) and is navigable from A to B and from B to A I thried to implement this in hibernate and it works well as long as the navigation goes only in one direction meaning: there is 1.) a public Collection getABAssociatins() method in the A class 2.) a public B getB() method in the Association class At the point where I try to include backwards navigation I run into the following error stack: net.sf.hibernate.WrongClassException: Object with id: 21 was not of the specified subclass: test.A (loaded object was of wrong class) at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:300) at net.sf.hibernate.loader.Loader.getRow(Loader.java:278) at net.sf.hibernate.loader.Loader.doFind(Loader.java:159) at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:602) at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:102) at net.sf.hibernate.impl.SessionImpl.initialize(SessionImpl.java:2897) at net.sf.hibernate.collection.PersistentCollection.getInitialValue(PersistentCollection.java:128) at net.sf.hibernate.type.PersistentCollectionType.getCollection(PersistentCollectionType.java:74) at net.sf.hibernate.type.PersistentCollectionType.resolveIdentifier(PersistentCollectionType.java:177) at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:1959) at net.sf.hibernate.loader.Loader.doFind(Loader.java:196) at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:602) at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:102) at net.sf.hibernate.impl.SessionImpl.initialize(SessionImpl.java:2897) at net.sf.hibernate.collection.PersistentCollection.getInitialValue(PersistentCollection.java:128) at net.sf.hibernate.type.PersistentCollectionType.getCollection(PersistentCollectionType.java:74) at net.sf.hibernate.type.PersistentCollectionType.resolveIdentifier(PersistentCollectionType.java:177) at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:1959) at net.sf.hibernate.loader.Loader.doFind(Loader.java:196) at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:587) at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:42) at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:396) at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:1889) at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1757) at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1688) at test.testEnitityToObjectToEntityReferenzes(HibernateTest.java:315) I suppose hibernat does not support this kind of usage (or am I doing something wrong???) and would like to propose this for an improvement. Kindest Regards Alexander Thomas

    Hibernate JIRA | 1 decade ago | alexander thomas
    net.sf.hibernate.WrongClassException: Object with id: 21 was not of the specified subclass: test.A (loaded object was of wrong class)

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. net.sf.hibernate.WrongClassException

      Object with id: XXX was not of the specified subclass: com.woolly.store.product.DisplayProductVariation (loaded object was of wrong class)

      at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded()
    2. net.sf.hibernate
      Loader.instanceAlreadyLoaded
      1. net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:531)
      1 frame