net.sf.hibernate.WrongClassException: Object with id: 12 was not of the specified subclass: com.redstarshop.data.File (loaded object was of wrong class)

Hibernate JIRA | Marko Nikolic | 1 decade 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

    I am having problem using Lightweight design pattern. I must admit that I am completely new to Hibernate but it seems to me like this is a bug of Hibernate. Here are my mappings: ### FileInfo.hbm.xml ### <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="com.redstarshop.data.FileInfo" table="files"> <id name="id" column="id" type="long"> <generator class="native"/> </id> <property name="name" column="name"/> <property name="contentType" column="content_type" not-null="true"/> <property name="length" not-null="true" column="content_length"/> <property name="lastModified" column="last_modified" not-null="true"/> </class> </hibernate-mapping> ### File.hbm.xml ### <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="com.redstarshop.data.File" table="files" polymorphism="explicit"> <id name="id" column="id" type="long"> <generator class="native"/> </id> <property name="name" column="name"/> <property name="contentType" column="content_type" not-null="true"/> <property name="length" not-null="true" column="content_length"/> <property name="lastModified" column="last_modified" not-null="true"/> <property name="content" not-null="true"/> </class> </hibernate-mapping> ### End of mappings ### Now, loading FileInfo (lightweight) entity works fine. But if I try to load File (heavyweight) entity (after FileInfo was loaded and cached) with the same id as loaded FileInfo, using either: session.load(File.class, id) or session.find("from com.redstarshop.data.File as c where c.id = ?", id, Hibernate.LONG) I always get net.sf.hibernate.WrongClassException: Object with id: 12 was not of the specified subclass: com.redstarshop.data.File (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.find(Loader.java:620) at net.sf.hibernate.hql.QueryTranslator.find(QueryTranslator.java:928) at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1343) at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1322) at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1318) at com.redstarshop.util.Utils.findOne(Utils.java:46) at com.redstarshop.tapestry.FileService.service(FileService.java:116) at org.apache.tapestry.engine.AbstractEngine.service(AbstractEngine.java:913) at org.apache.tapestry.ApplicationServlet.doService(ApplicationServlet.java:238) at org.apache.tapestry.ApplicationServlet.doGet(ApplicationServlet.java:199) at javax.servlet.http.HttpServlet.service(HttpServlet.java:740) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:366) at org.mortbay.jetty.servlet.WebApplicationHandler$Chain.doFilter(WebApplicationHandler.java:341) at org.apache.tapestry.RedirectFilter.doFilter(RedirectFilter.java:148) at org.mortbay.jetty.servlet.WebApplicationHandler$Chain.doFilter(WebApplicationHandler.java:333) at org.mortbay.jetty.servlet.WebApplicationHandler.dispatch(WebApplicationHandler.java:285) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:567) at org.mortbay.http.HttpContext.handle(HttpContext.java:1696) at org.mortbay.jetty.servlet.WebApplicationContext.handle(WebApplicationContext.java:552) at org.mortbay.http.HttpContext.handle(HttpContext.java:1646) at org.mortbay.http.HttpServer.service(HttpServer.java:884) at org.mortbay.http.HttpConnection.service(HttpConnection.java:799) at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:949) at org.mortbay.http.HttpConnection.handle(HttpConnection.java:816) at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:204) at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:290) at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:509) Following patch against 2.0.3 works fine for me (I patched loader to use fetched instance only if it is assignable to requested type). ### Begin of patch ### --- /cygdrive/c/java/hibernate-2.0.3/src/net/sf/hibernate/loader/Loader.java 2003-08-27 16:49:40.000000000 +0200 +++ hibernate-2.0.3/src/net/sf/hibernate/loader/Loader.java 2003-08-31 17:03:34.000000000 +0200 @@ -273,7 +273,7 @@ public abstract class Loader { //If the object is already loaded, return the loaded one object = session.getEntity(key); - if (object!=null) { + if (object!=null && persisters[i].getMappedClass().isAssignableFrom(object.getClass())) { //its already loaded so don't need to hydrate it instanceAlreadyLoaded(rs, i, persisters[i], suffixes[i], key, object, lockModes[i], session); } @@ -296,9 +296,6 @@ public abstract class Loader { private void instanceAlreadyLoaded(ResultSet rs, int i, Loadable persister, String suffix, Key key, Object object, LockMode lockMode, SessionImplementor session) throws HibernateException, SQLException { - if ( !persister.getMappedClass().isAssignableFrom( object.getClass() ) ) - throw new WrongClassException( "loaded object was of wrong class", key.getIdentifier(), persister.getMappedClass() ); - if ( LockMode.NONE!=lockMode && upgradeLocks() ) { //no point doing this if NONE was requested if ( ### End of patch ###

    Hibernate JIRA | 1 decade ago | Marko Nikolic
    net.sf.hibernate.WrongClassException: Object with id: 12 was not of the specified subclass: com.redstarshop.data.File (loaded object was of wrong class)
  2. 0

    I am having problem using Lightweight design pattern. I must admit that I am completely new to Hibernate but it seems to me like this is a bug of Hibernate. Here are my mappings: ### FileInfo.hbm.xml ### <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="com.redstarshop.data.FileInfo" table="files"> <id name="id" column="id" type="long"> <generator class="native"/> </id> <property name="name" column="name"/> <property name="contentType" column="content_type" not-null="true"/> <property name="length" not-null="true" column="content_length"/> <property name="lastModified" column="last_modified" not-null="true"/> </class> </hibernate-mapping> ### File.hbm.xml ### <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="com.redstarshop.data.File" table="files" polymorphism="explicit"> <id name="id" column="id" type="long"> <generator class="native"/> </id> <property name="name" column="name"/> <property name="contentType" column="content_type" not-null="true"/> <property name="length" not-null="true" column="content_length"/> <property name="lastModified" column="last_modified" not-null="true"/> <property name="content" not-null="true"/> </class> </hibernate-mapping> ### End of mappings ### Now, loading FileInfo (lightweight) entity works fine. But if I try to load File (heavyweight) entity (after FileInfo was loaded and cached) with the same id as loaded FileInfo, using either: session.load(File.class, id) or session.find("from com.redstarshop.data.File as c where c.id = ?", id, Hibernate.LONG) I always get net.sf.hibernate.WrongClassException: Object with id: 12 was not of the specified subclass: com.redstarshop.data.File (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.find(Loader.java:620) at net.sf.hibernate.hql.QueryTranslator.find(QueryTranslator.java:928) at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1343) at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1322) at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1318) at com.redstarshop.util.Utils.findOne(Utils.java:46) at com.redstarshop.tapestry.FileService.service(FileService.java:116) at org.apache.tapestry.engine.AbstractEngine.service(AbstractEngine.java:913) at org.apache.tapestry.ApplicationServlet.doService(ApplicationServlet.java:238) at org.apache.tapestry.ApplicationServlet.doGet(ApplicationServlet.java:199) at javax.servlet.http.HttpServlet.service(HttpServlet.java:740) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:366) at org.mortbay.jetty.servlet.WebApplicationHandler$Chain.doFilter(WebApplicationHandler.java:341) at org.apache.tapestry.RedirectFilter.doFilter(RedirectFilter.java:148) at org.mortbay.jetty.servlet.WebApplicationHandler$Chain.doFilter(WebApplicationHandler.java:333) at org.mortbay.jetty.servlet.WebApplicationHandler.dispatch(WebApplicationHandler.java:285) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:567) at org.mortbay.http.HttpContext.handle(HttpContext.java:1696) at org.mortbay.jetty.servlet.WebApplicationContext.handle(WebApplicationContext.java:552) at org.mortbay.http.HttpContext.handle(HttpContext.java:1646) at org.mortbay.http.HttpServer.service(HttpServer.java:884) at org.mortbay.http.HttpConnection.service(HttpConnection.java:799) at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:949) at org.mortbay.http.HttpConnection.handle(HttpConnection.java:816) at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:204) at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:290) at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:509) Following patch against 2.0.3 works fine for me (I patched loader to use fetched instance only if it is assignable to requested type). ### Begin of patch ### --- /cygdrive/c/java/hibernate-2.0.3/src/net/sf/hibernate/loader/Loader.java 2003-08-27 16:49:40.000000000 +0200 +++ hibernate-2.0.3/src/net/sf/hibernate/loader/Loader.java 2003-08-31 17:03:34.000000000 +0200 @@ -273,7 +273,7 @@ public abstract class Loader { //If the object is already loaded, return the loaded one object = session.getEntity(key); - if (object!=null) { + if (object!=null && persisters[i].getMappedClass().isAssignableFrom(object.getClass())) { //its already loaded so don't need to hydrate it instanceAlreadyLoaded(rs, i, persisters[i], suffixes[i], key, object, lockModes[i], session); } @@ -296,9 +296,6 @@ public abstract class Loader { private void instanceAlreadyLoaded(ResultSet rs, int i, Loadable persister, String suffix, Key key, Object object, LockMode lockMode, SessionImplementor session) throws HibernateException, SQLException { - if ( !persister.getMappedClass().isAssignableFrom( object.getClass() ) ) - throw new WrongClassException( "loaded object was of wrong class", key.getIdentifier(), persister.getMappedClass() ); - if ( LockMode.NONE!=lockMode && upgradeLocks() ) { //no point doing this if NONE was requested if ( ### End of patch ###

    Hibernate JIRA | 1 decade ago | Marko Nikolic
    net.sf.hibernate.WrongClassException: Object with id: 12 was not of the specified subclass: com.redstarshop.data.File (loaded object was of wrong class)
  3. 0

    Hibernate WrongClassException ,table per class scheme

    Google Groups | 1 decade ago | Elhanan
    net.sf.hibernate.WrongClassException: Object with id: 37 was not of the specified subclass: com.pnx.underwrite.bo.underwritemodel.PursuitImpl (loaded object was of wrong class)
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    i got this error in my project , please can anybody help me? i have an inheritance mapping , you can see the class diagrama in the attachment. i want to map the sell class, there are two references , one to the client class and the other one to the employment class. this is the mapping that i built <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="Inventory.Sell" table="Sell"> <id name="Id_Sell" type="integer"> <column name="Id_Sell" /> <generator class="identity" /> </id> <property name="FechaPedido" type="date"> <column name="FechaPedido" /> </property> <many-to-one name="Id_Cliente" class="Inventory.Client" > <column name="Id_Cliente" not-null="false" /> </many-to-one> <many-to-one name="Id_Employment" class="Inventory.Employment" > <column name="Id_Employment" not-null="false" /> </many-to-one> </class> </hibernate-mapping> and this is the mapping for the inheritance <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="Inventory.Person" table="person" > <id name="Id_Persona" type="integer"> <column name="Id_Persona" /> <generator class="identity" /> </id> <property name="CI" type="string"> <column name="CI" length="20" /> </property> <property name="nombre" type="string"> <column name="Nombre" length="100" /> </property> <property name="trabajoDir" type="string"> <column name="DireccionTrabajo" length="100" /> </property> <property name="DireccionDomicilio" type="string"> <column name="DireccionDomicilio" length="100" /> </property> <property name="Celular" type="string"> <column name="Celular" length="100" /> </property> <set name="emailAddresses" table="email"> <key column="Id_Email"/> <element type="string" column="Email"/> </set> <set name="telefono" table="telefono"> <key column="Id_Telefono"/> <element type="string" column="Telefono"/> </set> <set name="fax" table="fax"> <key column="Id_Fax"/> <element type="string" column="Fax"/> </set> <joined-subclass name="Inventory.Employment" table="employment"> <key column="Id_Empleado"/> <property name="Sueldo" type="string"> <column name="Sueldo" length="100" /> </property> <property name="Moneda" type="string"> <column name="Moneda" length="100" /> </property> <property name="FechaContrato" type="date"> <column name="FechaContrato" /> </property> </joined-subclass> <joined-subclass name="Inventory.Client" table="client"> <key column="Id_Cliente"/> </joined-subclass> </class> </hibernate-mapping> this is the error that i get: net.sf.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: ...................................... at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:531) at net.sf.hibernate.loader.Loader.getRow(Loader.java:498) at net.sf.hibernate.loader.Loader.getRowFromResultSet(Loader.java:213) at net.sf.hibernate.loader.Loader.doQuery(Loader.java:281) at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133) at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:990) at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:965) at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:93) at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:288) at net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3303) at net.sf.hibernate.collection.PersistentCollection.forceInitialization(PersistentCollection.java:336) at net.sf.hibernate.impl.SessionImpl.initializeNonLazyCollections(SessionImpl.java:3156) at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138) at net.sf.hibernate.loader.Loader.doList(Loader.java:1033) at net.sf.hibernate.loader.Loader.list(Loader.java:1024) at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854) at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1553) at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1530) at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1522) at hibernate.conexion.consulta(conexion.java:177) at _Control__Acceso._jspService(_Control__Acceso.java:80) [/Control_Acceso.jsp] at com.orionserver[Oracle Containers for J2EE 10g (10.1.3.1.0) ].http.OrionHttpJspPage.service(OrionHttpJspPage.java:59) at oracle.jsp.runtimev2.JspPageTable.service(JspPageTable.java:453) at oracle.jsp.runtimev2.JspServlet.internalService(JspServlet.java:591) at oracle.jsp.runtimev2.JspServlet.service(JspServlet.java:515) at javax.servlet.http.HttpServlet.service(HttpServlet.java:856) at com.evermind[Oracle Containers for J2EE 10g (10.1.3.1.0) ].server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:711) at com.evermind[Oracle Containers for J2EE 10g (10.1.3.1.0) ].server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:368) at com.evermind[Oracle Containers for J2EE 10g (10.1.3.1.0) ].server.http.HttpRequestHandler.doProcessRequest(HttpRequestHandler.java:866) at com.evermind[Oracle Containers for J2EE 10g (10.1.3.1.0) ].server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:448) at com.evermind[Oracle Containers for J2EE 10g (10.1.3.1.0) ].server.http.HttpRequestHandler.serveOneRequest(HttpRequestHandler.java:216) at com.evermind[Oracle Containers for J2EE 10g (10.1.3.1.0) ].server.http.HttpRequestHandler.run(HttpRequestHandler.java:117) at com.evermind[Oracle Containers for J2EE 10g (10.1.3.1.0) ].server.http.HttpRequestHandler.run(HttpRequestHandler.java:110) at oracle.oc4j.network.ServerSocketReadHandler$SafeRunnable.run(ServerSocketReadHandler.java:260) at oracle.oc4j.network.ServerSocketAcceptHandler.procClientSocket(ServerSocketAcceptHandler.java:239) at oracle.oc4j.network.ServerSocketAcceptHandler.access$700(ServerSocketAcceptHandler.java:34) at oracle.oc4j.network.ServerSocketAcceptHandler$AcceptHandlerHorse.run(ServerSocketAcceptHandler.java:880) at com.evermind[Oracle Containers for J2EE 10g (10.1.3.1.0) ].util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:303) at java.lang.Thread.run(Thread.java:595)

    Hibernate JIRA | 9 years ago | cristian vargas
    net.sf.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: ......................................

    Root Cause Analysis

    1. net.sf.hibernate.WrongClassException

      Object with id: 12 was not of the specified subclass: com.redstarshop.data.File (loaded object was of wrong class)

      at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded()
    2. net.sf.hibernate
      SessionImpl.find
      1. net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:300)
      2. net.sf.hibernate.loader.Loader.getRow(Loader.java:278)
      3. net.sf.hibernate.loader.Loader.doFind(Loader.java:159)
      4. net.sf.hibernate.loader.Loader.find(Loader.java:620)
      5. net.sf.hibernate.hql.QueryTranslator.find(QueryTranslator.java:928)
      6. net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1343)
      7. net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1322)
      8. net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1318)
      8 frames
    3. com.redstarshop.util
      Utils.findOne
      1. com.redstarshop.util.Utils.findOne(Utils.java:46)
      1 frame
    4. com.redstarshop.tapestry
      FileService.service
      1. com.redstarshop.tapestry.FileService.service(FileService.java:116)
      1 frame
    5. org.apache.tapestry
      ApplicationServlet.doGet
      1. org.apache.tapestry.engine.AbstractEngine.service(AbstractEngine.java:913)
      2. org.apache.tapestry.ApplicationServlet.doService(ApplicationServlet.java:238)
      3. org.apache.tapestry.ApplicationServlet.doGet(ApplicationServlet.java:199)
      3 frames
    6. JavaServlet
      HttpServlet.service
      1. javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
      2. javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      2 frames
    7. Jetty Server
      WebApplicationHandler$Chain.doFilter
      1. org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:366)
      2. org.mortbay.jetty.servlet.WebApplicationHandler$Chain.doFilter(WebApplicationHandler.java:341)
      2 frames
    8. org.apache.tapestry
      RedirectFilter.doFilter
      1. org.apache.tapestry.RedirectFilter.doFilter(RedirectFilter.java:148)
      1 frame
    9. Jetty Server
      ServletHandler.handle
      1. org.mortbay.jetty.servlet.WebApplicationHandler$Chain.doFilter(WebApplicationHandler.java:333)
      2. org.mortbay.jetty.servlet.WebApplicationHandler.dispatch(WebApplicationHandler.java:285)
      3. org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:567)
      3 frames
    10. org.mortbay.http
      HttpContext.handle
      1. org.mortbay.http.HttpContext.handle(HttpContext.java:1696)
      1 frame
    11. Jetty Server
      WebApplicationContext.handle
      1. org.mortbay.jetty.servlet.WebApplicationContext.handle(WebApplicationContext.java:552)
      1 frame
    12. org.mortbay.http
      SocketListener.handleConnection
      1. org.mortbay.http.HttpContext.handle(HttpContext.java:1646)
      2. org.mortbay.http.HttpServer.service(HttpServer.java:884)
      3. org.mortbay.http.HttpConnection.service(HttpConnection.java:799)
      4. org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:949)
      5. org.mortbay.http.HttpConnection.handle(HttpConnection.java:816)
      6. org.mortbay.http.SocketListener.handleConnection(SocketListener.java:204)
      6 frames
    13. Jetty Util
      ThreadPool$PoolThread.run
      1. org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:290)
      2. org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:509)
      2 frames