原创

org.hibernate.WrongClassException: Object with id: 2 was not of the specified subclass

问题分析以及解决方案:

有三张表分别是TA、TB、TC,其中TA是主表,TB与TC是2个子表;这3个表分别对应A、B、C3个Java类,

若TA表中存在一条主键是001的记录,且同时在TB、TC两张表中都存在一条主键是001的记录

(注意:正常操作不会出现这样的记录,一般是在开发过程中手动修改数据而且数据库也认为这样数据是合理的)此时如果主键为001的B对象,hibernate就会跑出上述异常信息。


关于 org.hibernate.WrongClassException 错误的产生原因分析,具体错误信息如下:

SEVERE: Exception starting filter springSecurityFilterChain

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servicesManager' defined in ServletContext resource [/WEB-INF/spring-configuration/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.jasig.cas.services.DefaultServicesManagerImpl]: Constructor threw exception; nested exception is org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: Object with id: 2 was not of the specified subclass: org.jasig.cas.services.AbstractRegisteredService (Discriminator: 4); nested exception is org.hibernate.WrongClassException: Object with id: 2 was not of the specified subclass: org.jasig.cas.services.AbstractRegisteredService (Discriminator: 4)

at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:288)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1035)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:939)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)

at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)

at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)

at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)

at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)

at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)

at org.jasig.cas.web.init.SafeContextLoaderListener.contextInitialized_aroundBody0(SafeContextLoaderListener.java:75)

at org.jasig.cas.web.init.SafeContextLoaderListener.contextInitialized_aroundBody1$advice(SafeContextLoaderListener.java:57)

at org.jasig.cas.web.init.SafeContextLoaderListener.contextInitialized(SafeContextLoaderListener.java:1)

at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)

at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)

at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)

at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)

at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)

at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1247)

at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1897)

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)

at java.util.concurrent.FutureTask.run(FutureTask.java:262)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

at java.lang.Thread.run(Thread.java:745)

Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.jasig.cas.services.DefaultServicesManagerImpl]: Constructor threw exception; nested exception is org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: Object with id: 2 was not of the specified subclass: org.jasig.cas.services.AbstractRegisteredService (Discriminator: 4); nested exception is org.hibernate.WrongClassException: Object with id: 2 was not of the specified subclass: org.jasig.cas.services.AbstractRegisteredService (Discriminator: 4)

at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)

at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:110)

at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)

... 30 more

Caused by: org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: Object with id: 2 was not of the specified subclass: org.jasig.cas.services.AbstractRegisteredService (Discriminator: 4); nested exception is org.hibernate.WrongClassException: Object with id: 2 was not of the specified subclass: org.jasig.cas.services.AbstractRegisteredService (Discriminator: 4)

at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:680)

at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:104)

at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)

at org.springframework.orm.jpa.JpaAccessor.translateIfNecessary(JpaAccessor.java:155)

at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:192)

at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:150)

at org.springframework.orm.jpa.JpaTemplate.find(JpaTemplate.java:310)

at org.springframework.orm.jpa.JpaTemplate.find(JpaTemplate.java:306)

at org.jasig.cas.services.JpaServiceRegistryDaoImpl.load_aroundBody2(JpaServiceRegistryDaoImpl.java:43)

at org.jasig.cas.services.JpaServiceRegistryDaoImpl.load_aroundBody3$advice(JpaServiceRegistryDaoImpl.java:57)

at org.jasig.cas.services.JpaServiceRegistryDaoImpl.load(JpaServiceRegistryDaoImpl.java:1)

at org.jasig.cas.services.DefaultServicesManagerImpl.load(DefaultServicesManagerImpl.java:147)

at org.jasig.cas.services.DefaultServicesManagerImpl.<init>(DefaultServicesManagerImpl.java:71)

at org.jasig.cas.services.DefaultServicesManagerImpl.<init>(DefaultServicesManagerImpl.java:57)

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)

... 32 more

Caused by: org.hibernate.WrongClassException: Object with id: 2 was not of the specified subclass: org.jasig.cas.services.AbstractRegisteredService (Discriminator: 4)

at org.hibernate.loader.Loader.getInstanceClass(Loader.java:1615)

at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1449)

at org.hibernate.loader.Loader.getRow(Loader.java:1373)

at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:640)

at org.hibernate.loader.Loader.doQuery(Loader.java:850)

at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)

at org.hibernate.loader.Loader.doList(Loader.java:2447)

at org.hibernate.loader.Loader.doList(Loader.java:2433)

at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2263)

at org.hibernate.loader.Loader.list(Loader.java:2258)

at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470)

at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)

at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:196)

at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1161)

at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)

at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:252)

at org.springframework.orm.jpa.JpaTemplate$9.doInJpa(JpaTemplate.java:319)

at org.springframework.orm.jpa.JpaTemplate$9.doInJpa(JpaTemplate.java:1)

at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:187)

... 46 more


造成此问题的原因是两个子表中存在相同主键的记录,删除手动添加的数据即可。

关注下方微信公众号“Java精选”(w_z90110),回复关键字领取资料:如HadoopDubboCAS源码等等,免费领取资料视频和项目。 

涵盖:程序人生、搞笑视频、算法与数据结构、黑客技术与网络安全、前端开发、Java、Python、Redis缓存、Spring源码、各大主流框架、Web开发、大数据技术、Storm、Hadoop、MapReduce、Spark、elasticsearch、单点登录统一认证、分布式框架、集群、安卓开发、iOS开发、C/C++、.NET、Linux、Mysql、Oracle、NoSQL非关系型数据库、运维等。

评论

分享:

支付宝

微信