net.sf.hibernate.WrongClassException: Object with id: 1346 was not of the specified subclass: my.BarImpl(loaded object was of wrong class)

Hibernate JIRA | Sam Perman | 1 decade ago
tip
Do you know that we can give you better hits? Get more relevant results from Samebug’s stack trace search.
  1. 0

    I have a table that contains elements of different types... for example: my_table ( id (a long) type (and enumerated type, eithe 'foo' or 'bar') ) they can be link together in a link table my_link_table ( parent_id child_id ) I have three mappings (one parent, and two subclasses): <hibernate-mapping> <class name="my.ParentImpl" table="my_table" discriminator-value="null" mutable="false"> <cache usage="read-only"/> <id name="mId" column="id" type="long" access="field"> <generator class="sequence"> <param name="sequence">foo_seq</param> </generator> </id> <discriminator column="type" type="string"/> <subclass name="my.FooImpl" discriminator-value="foo"> </subclass> <subclass name="my.BarImpl" disciminator-value="bar"> <set name="parentFoos" table="my_link_table" access="field" lazy="true"> <key column="child_id"/> <many-to-many column="parent_id" class="my.FooImpl"/> </set> </subclass> </hibernate-mapping> At this point, I load an instance of BarImpl and I access the "parentFoos" collection. My hope is that it only reads values from "my_link_table" if the object referenced by "parent_id" has the correct discriminator-value. Unfortunately, even if the parent_id has a discriminator-value of "bar" it will read it from the database AND create a FooImpl with that data. This results in an entry in the cache where there is a FooImpl in the cache for an id that should actually be a BarImpl. This means that the next time I get the BarImpl, I get the following exception: ERROR 2004-05-27 15:05:59,453 my.FooService - Unable to retrieve related styles for: my.BarImpl@2d4c7e net.sf.hibernate.WrongClassException: Object with id: 1346 was not of the specified subclass: my.BarImpl(loaded object was of wrong class) at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:456) at net.sf.hibernate.loader.Loader.getRow(Loader.java:423) at net.sf.hibernate.loader.Loader.doQuery(Loader.java:209) at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133) at net.sf.hibernate.loader.Loader.doList(Loader.java:955) at net.sf.hibernate.loader.Loader.list(Loader.java:946) at net.sf.hibernate.loader.SQLLoader.list(SQLLoader.java:92) at net.sf.hibernate.impl.SessionImpl.findBySQL(SessionImpl.java:3797) at net.sf.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:52) at my.BarImpl.getParents(BarImpl.java:266) The sql that hibernate uses to initially populate the collection is: select parentf0_.parent_id as parent_1___, parentf0_.child_id as child_2___ from my_link_table parentf0_ where parentf0_.child_id=1003 I think this should add a where clause (and an additional "from" table) something like: AND parentf0_.parent_id = my_table.parent_id AND my_table.type='foo'

    Hibernate JIRA | 1 decade ago | Sam Perman
    net.sf.hibernate.WrongClassException: Object with id: 1346 was not of the specified subclass: my.BarImpl(loaded object was of wrong class)
  2. 0

    I have a table that contains elements of different types... for example: my_table ( id (a long) type (and enumerated type, eithe 'foo' or 'bar') ) they can be link together in a link table my_link_table ( parent_id child_id ) I have three mappings (one parent, and two subclasses): <hibernate-mapping> <class name="my.ParentImpl" table="my_table" discriminator-value="null" mutable="false"> <cache usage="read-only"/> <id name="mId" column="id" type="long" access="field"> <generator class="sequence"> <param name="sequence">foo_seq</param> </generator> </id> <discriminator column="type" type="string"/> <subclass name="my.FooImpl" discriminator-value="foo"> </subclass> <subclass name="my.BarImpl" disciminator-value="bar"> <set name="parentFoos" table="my_link_table" access="field" lazy="true"> <key column="child_id"/> <many-to-many column="parent_id" class="my.FooImpl"/> </set> </subclass> </hibernate-mapping> At this point, I load an instance of BarImpl and I access the "parentFoos" collection. My hope is that it only reads values from "my_link_table" if the object referenced by "parent_id" has the correct discriminator-value. Unfortunately, even if the parent_id has a discriminator-value of "bar" it will read it from the database AND create a FooImpl with that data. This results in an entry in the cache where there is a FooImpl in the cache for an id that should actually be a BarImpl. This means that the next time I get the BarImpl, I get the following exception: ERROR 2004-05-27 15:05:59,453 my.FooService - Unable to retrieve related styles for: my.BarImpl@2d4c7e net.sf.hibernate.WrongClassException: Object with id: 1346 was not of the specified subclass: my.BarImpl(loaded object was of wrong class) at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:456) at net.sf.hibernate.loader.Loader.getRow(Loader.java:423) at net.sf.hibernate.loader.Loader.doQuery(Loader.java:209) at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133) at net.sf.hibernate.loader.Loader.doList(Loader.java:955) at net.sf.hibernate.loader.Loader.list(Loader.java:946) at net.sf.hibernate.loader.SQLLoader.list(SQLLoader.java:92) at net.sf.hibernate.impl.SessionImpl.findBySQL(SessionImpl.java:3797) at net.sf.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:52) at my.BarImpl.getParents(BarImpl.java:266) The sql that hibernate uses to initially populate the collection is: select parentf0_.parent_id as parent_1___, parentf0_.child_id as child_2___ from my_link_table parentf0_ where parentf0_.child_id=1003 I think this should add a where clause (and an additional "from" table) something like: AND parentf0_.parent_id = my_table.parent_id AND my_table.type='foo'

    Hibernate JIRA | 1 decade ago | Sam Perman
    net.sf.hibernate.WrongClassException: Object with id: 1346 was not of the specified subclass: my.BarImpl(loaded object was of wrong class)

    Root Cause Analysis

    1. net.sf.hibernate.WrongClassException

      Object with id: 1346 was not of the specified subclass: my.BarImpl(loaded object was of wrong class)

      at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded()
    2. net.sf.hibernate
      SQLQueryImpl.list
      1. net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:456)
      2. net.sf.hibernate.loader.Loader.getRow(Loader.java:423)
      3. net.sf.hibernate.loader.Loader.doQuery(Loader.java:209)
      4. net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
      5. net.sf.hibernate.loader.Loader.doList(Loader.java:955)
      6. net.sf.hibernate.loader.Loader.list(Loader.java:946)
      7. net.sf.hibernate.loader.SQLLoader.list(SQLLoader.java:92)
      8. net.sf.hibernate.impl.SessionImpl.findBySQL(SessionImpl.java:3797)
      9. net.sf.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:52)
      9 frames
    3. my
      BarImpl.getParents
      1. my.BarImpl.getParents(BarImpl.java:266)
      1 frame