一、异常描述
启动项目时报出“org.hibernate.InstantiationException: No default constructor for entity”异常,具体如下所示
org.hibernate.InstantiationException: No default constructor for entity: com.woopa.sf.entity.User@b@ at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:120)@b@ at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:136)@b@ at org.hibernate.tuple.entity.AbstractEntityTuplizer.instantiate(AbstractEntityTuplizer.java:730)@b@ at org.hibernate.persister.entity.AbstractEntityPersister.instantiate(AbstractEntityPersister.java:4552)@b@ at org.hibernate.internal.SessionImpl.instantiate(SessionImpl.java:1388)@b@ at org.hibernate.internal.SessionImpl.instantiate(SessionImpl.java:1377)@b@ at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1596)@b@ at org.hibernate.loader.Loader.getRow(Loader.java:1505)@b@ at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:713)@b@ at org.hibernate.loader.Loader.processResultSet(Loader.java:943)@b@ at org.hibernate.loader.Loader.doQuery(Loader.java:911)@b@ at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)@b@ at org.hibernate.loader.Loader.doList(Loader.java:2526)@b@ at org.hibernate.loader.Loader.doList(Loader.java:2512)@b@ at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342)@b@ at org.hibernate.loader.Loader.list(Loader.java:2337)@b@ at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495)@b@ at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:356)@b@ at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)@b@ at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1269)@b@ at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)@b@ at com.woopa.common.persist.hibernate4.BaseHibernateDao.find(BaseHibernateDao.java:692)@b@ at com.woopa.common.persist.hibernate4.BaseHibernateDao.getUnique(BaseHibernateDao.java:643)@b@ at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)@b@ at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)@b@ at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)@b@ at java.lang.reflect.Method.invoke(Method.java:597)@b@ at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)@b@ at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)@b@ at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)@b@ at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)@b@ at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)@b@ at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)@b@ at sun.proxy.$Proxy30.getUnique(Unknown Source)@b@ at com.woopa.sf.service.UserService.getByUsername(UserService.java:171)@b@ at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)@b@ at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)@b@ at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)@b@ at java.lang.reflect.Method.invoke(Method.java:597)@b@ at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)@b@ at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)@b@ at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)@b@ at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)@b@ at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:51)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)@b@ at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)@b@ at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)@b@ at sun.proxy.$Proxy49.getByUsername(Unknown Source)@b@ at com.woopa.sf.service.UserLoginProcessorImpl.doLogin(UserLoginProcessorImpl.java:24)@b@ at com.woopa.sf.ex.AbstractUserLoginProcessor.login(AbstractUserLoginProcessor.java:23)@b@ at com.woopa.sf.sec.JdbcUserDetailsService.loadUserByUsername(JdbcUserDetailsService.java:30)@b@ at org.springframework.security.providers.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:83)@b@ at org.springframework.security.providers.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:121)@b@ at org.springframework.security.providers.ProviderManager.doAuthentication(ProviderManager.java:188)@b@ at org.springframework.security.AbstractAuthenticationManager.authenticate(AbstractAuthenticationManager.java:46)@b@ at org.springframework.security.ui.webapp.AuthenticationProcessingFilter.attemptAuthentication(AuthenticationProcessingFilter.java:82)@b@ at org.springframework.security.ui.AbstractProcessingFilter.doFilterHttp(AbstractProcessingFilter.java:259)@b@ at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)@b@ at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)@b@ at org.springframework.security.ui.logout.LogoutFilter.doFilterHttp(LogoutFilter.java:89)@b@ at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)@b@ at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)@b@ at org.springframework.security.context.HttpSessionContextIntegrationFilter.doFilterHttp(HttpSessionContextIntegrationFilter.java:235)@b@ at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)@b@ at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)@b@ at org.springframework.security.concurrent.ConcurrentSessionFilter.doFilterHttp(ConcurrentSessionFilter.java:99)@b@ at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)@b@ at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)@b@ at org.springframework.security.util.FilterChainProxy.doFilter(FilterChainProxy.java:175)@b@ at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)@b@ at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)@b@ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)@b@ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)@b@ at org.ajaxanywhere.AAFilter.doFilter(AAFilter.java:46)@b@ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)@b@ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)@b@ at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)@b@ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)@b@ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)@b@ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)@b@ at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)@b@ at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)@b@ at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)@b@ at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)@b@ at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)@b@ at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)@b@ at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)@b@ at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)@b@ at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)@b@ at java.lang.Thread.run(Thread.java:662)
二、解决方法
1. 确认spring的LocalSessionFactoryBean配置packagesToScan是否包含com.woopa.sf.entity.User包路径
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >@b@ <property name="dataSource">@b@ <ref local="dataSource"/>@b@ </property>@b@ <property name="hibernateProperties">@b@ <props>@b@ <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>@b@ <prop key="hibernate.dialect">${m_hibernate.dialect}</prop>@b@ <prop key="hibernate.cache.use_query_cache">false</prop>@b@ <prop key="hibernate.cache.use_second_level_cache">false</prop>@b@ <prop key="hibernate.show_sql">true</prop>@b@ <prop key="hibernate.format_sql">false</prop>@b@ <prop key="hibernate.jdbc.fetch_size">50</prop>@b@ <prop key="hibernate.jdbc.batch_size">30</prop>@b@ <prop key="hibernate.query.factory_class">org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory</prop>@b@ </props>@b@ </property> @b@ <property name="packagesToScan">@b@ <list>@b@ <value>com.woopa.**.bean</value>@b@ <value>com.woopa.**.entity</value>@b@ <value>com.woopa.**.pojo</value>@b@ <value>com.woopa.**.model</value>@b@ </list>@b@ </property>@b@ </bean>
2.发现对于User的POJO类中包含有构造函数如下所示,应该去除构造函数代码块,去掉问题解决了
@Entity@b@@Table(name="T_USER")@b@@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)@b@public class User implements UserData, Cloneable {@b@@b@...@b@@b@/**应该删除构造函数代码段*/@b@ public User(String username, String password) {@b@ super();@b@ this.username = username;@b@ this.password = password;@b@ }@b@ /**end*/@b@...@b@@b@}