1、在spring 与hibernate 集成时出现下列错误
Exception in thread "main" : Error creating bean with name 'userDAO': Injection of resource dependencies failed; nested exception is : Error creating bean with name 'sessionFactory' defined in class path resource [beans.xml]: Error setting property values; nested exception is : Invalid property 'annotatedClasses' of bean class
[org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Bean property 'annotatedClasses' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at
org.springframework.context.annotation.CommonAnnotationBeanPos tProcessor.postProcessPropertyValues()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.populateBean()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.doCreateBean()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.createBean()
at
org.springframework.beans.factory.support.AbstractBeanFactory$
1.getObject()
at
org.springframework.beans.factory.support.DefaultSingletonBean Registry.getSingleton() at
org.springframework.beans.factory.support.AbstractBeanFactory. doGetBean()
at
org.springframework.beans.factory.support.AbstractBeanFactory.
getBean()
at
org.springframework.beans.factory.support.DefaultListableBeanF actory.preInstantiateSingletons()
at
org.springframework.context.support.AbstractApplicationContext .finishBeanFactoryInitialization()
at
org.springframework.context.support.AbstractApplicationContext .refresh()
at
org.springframework.context.support.ClassPathXmlApplicationCon text.()
at
org.springframework.context.support.ClassPathXmlApplicationCon text.()
atcom.spring.test.SpringTest.main() Caused by: : Error creating bean with name 'sessionFactory' defined in class path resource [beans.xml]: Error setting property values; nested exception is : Invalid property 'annotatedClasses' of bean class
[org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Bean property 'annotatedClasses' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.applyPropertyValues()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.populateBean()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.doCreateBean()
at
org.springframework.beans.factory.support.AbstractAutowireCapa
bleBeanFactory.createBean()
at
org.springframework.beans.factory.support.AbstractBeanFactory$
1.getObject()
at
org.springframework.beans.factory.support.DefaultSingletonBean Registry.getSingleton() at
org.springframework.beans.factory.support.AbstractBeanFactory. doGetBean()
at
org.springframework.beans.factory.support.AbstractBeanFactory. getBean()
at
org.springframework.context.annotation.CommonAnnotationBeanPos tProcessor.autowireResource()
at
org.springframework.context.annotation.CommonAnnotationBeanPos tProcessor.getResource()
at
org.springframework.context.annotation.CommonAnnotationBeanPos tProcessor$ResourceElement.getResourceToInject()
at
org.springframework.beans.factory.annotation.InjectionMetadata $InjectedElement.inject()
at
org.springframework.beans.factory.annotation.InjectionMetadata .inject()
at
org.springframework.context.annotation.CommonAnnotationBeanPos tProcessor.postProcessPropertyValues()
... 13 more
Caused by: : Invalid property 'annotatedClasses' of bean class
[org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Bean property 'annotatedClasses' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at
org.springframework.beans.BeanWrapperImpl.setPropertyValue()
at
org.springframework.beans.BeanWrapperImpl.setPropertyValue()
at
org.springframework.beans.AbstractPropertyAccessor.setProperty Values()
at
org.springframework.beans.AbstractPropertyAccessor.setProperty Values()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.applyPropertyValues()
... 26 more
原因是,在选择Session 工程类时,选择的工厂类与PO 映射方式不一致,Hibernate 有两个Session 工厂:
1、LocalSessionFactoryBean :通过XX.hbm.xml 来映射实体类
2、AnnotationSessionFactoryBean :通过注释来映射实体类
选择第一种做Session 工厂时,主要通过以下方式添加PO
选择第二种做Session 工厂时,主要通过以下方式添加PO
com.test.entity.TPerson
2、在往数据库中插入数据时出现以下错误:
Exception in thread "main"
: could not insert:
[com.spring.model.User]
at
org.hibernate.exception.SQLStateConverter.handledNonSpecificEx ception()
at
org.hibernate.exception.SQLStateConverter.convert()
at
org.hibernate.exception.JDBCExceptionHelper.convert()
at
org.hibernate.id.insert.AbstractReturningDelegate.performInser t()
at
org.hibernate.persister.entity.AbstractEntityPersister.insert()
atorg.hibernate.persister.entity.AbstractEntityPersister.in sert()
at
org.hibernate.action.EntityIdentityInsertAction.execute()
atorg.hibernate.engine.ActionQueue.execute()
at
org.hibernate.event.def.AbstractSaveEventListener.performSaveO rReplicate()
at
org.hibernate.event.def.AbstractSaveEventListener.performSave()
at
org.hibernate.event.def.AbstractSaveEventListener.saveWithGene ratedId()
at
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveW ithGeneratedOrRequestedId()
at
org.hibernate.event.def.DefaultSaveEventListener.saveWithGener atedOrRequestedId()
at
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entit yIsTransient() at
org.hibernate.event.def.DefaultSaveEventListener.performSaveOr Update()
at
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSav eOrUpdate()
atorg.hibernate.impl.SessionImpl.fireSave()
atorg.hibernate.impl.SessionImpl.save() atorg.hibernate.impl.SessionImpl.save() atcom.spring.DAOImpl.UserDAOImpl.saveUser()
atcom.spring.service.UserService.saveUserDAO()
atcom.spring.test.SpringTest.main()
Caused by: : Field 'id' doesn't have a default value
atcom.mysql.jdbc.SQLError.createSQLException()
atcom.mysql.jdbc.MysqlIO.checkErrorPacket()
atcom.mysql.jdbc.MysqlIO.checkErrorPacket()
atcom.mysql.jdbc.MysqlIO.sendCommand() atcom.mysql.jdbc.MysqlIO.sqlQueryDirect() atcom.mysql.jdbc.ConnectionImpl.execSQL()
at
com.mysql.jdbc.PreparedStatement.executeInternal()
at
com.mysql.jdbc.PreparedStatement.executeUpdate()
at
com.mysql.jdbc.PreparedStatement.executeUpdate()
at
com.mysql.jdbc.PreparedStatement.executeUpdate()
at
org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpd ate()
at
org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpd ate()
at
org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract()
at
org.hibernate.id.insert.AbstractReturningDelegate.performInser t()
... 18 more
原因是在代码中设置了主键自动增长,而在数据库表中并没有设置主键列自增
3、在spring 与hibernate 集成时出现下列错误
Exception in thread "main" : Error creating bean with name 'userService' defined in file
[E:\software\Myeclipse
10.6\workspace\Spring-11Spring_Hibernate_Transaction\bin\com\spring\service\UserService.class]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError:
org/objectweb/asm/Type
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.doCreateBean()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.createBean()
at
org.springframework.beans.factory.support.AbstractBeanFactory$
1.getObject()
at
org.springframework.beans.factory.support.DefaultSingletonBean Registry.getSingleton() at
org.springframework.beans.factory.support.AbstractBeanFactory. doGetBean()
at
org.springframework.beans.factory.support.AbstractBeanFactory. getBean()
at
org.springframework.beans.factory.support.DefaultListableBeanF actory.preInstantiateSingletons()
at
org.springframework.context.support.AbstractApplicationContext .finishBeanFactoryInitialization()
at
org.springframework.context.support.AbstractApplicationContext .refresh()
at
org.springframework.context.support.ClassPathXmlApplicationCon text.()
at
org.springframework.context.support.ClassPathXmlApplicationCon text.()
atcom.spring.test.SpringTest.main() Caused by: java.lang.NoClassDefFoundError:
org/objectweb/asm/Type
atnet.sf.cglib.core.TypeUtils.parseType() atnet.sf.cglib.core.KeyFactory.() atnet.sf.cglib.proxy.Enhancer.() at
org.springframework.aop.framework.Cglib2AopProxy.createEnhance
r()
at
org.springframework.aop.framework.Cglib2AopProxy.getProxy()
at
org.springframework.aop.framework.ProxyFactory.getProxy()
at
org.springframework.aop.framework.autoproxy.AbstractAutoProxyC reator.createProxy()
at
org.springframework.aop.framework.autoproxy.AbstractAutoProxyC reator.wrapIfNecessary() at
org.springframework.aop.framework.autoproxy.AbstractAutoProxyC reator.postProcessAfterInitialization()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.applyBeanPostProcessorsAfterInitialization()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.initializeBean()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.doCreateBean()
... 11 more
Caused by: :
org.objectweb.asm.Type
at java.net.URLClassLoader$1.run() atjava.security.AccessController.doPrivileged() atjava.net.URLClassLoader.findClass()
atjava.lang.ClassLoader.loadClass() atsun.misc.Launcher$AppClassLoader.loadClass()
atjava.lang.ClassLoader.loadClass() atjava.lang.ClassLoader.loadClassInternal()
... 23 more
解决方法:
把Hibernate 的cglib.jar 换成spring 的
cglib.jar(com.springsource.net.sf.cglib-2.2.0.jar)
1、在spring 与hibernate 集成时出现下列错误
Exception in thread "main" : Error creating bean with name 'userDAO': Injection of resource dependencies failed; nested exception is : Error creating bean with name 'sessionFactory' defined in class path resource [beans.xml]: Error setting property values; nested exception is : Invalid property 'annotatedClasses' of bean class
[org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Bean property 'annotatedClasses' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at
org.springframework.context.annotation.CommonAnnotationBeanPos tProcessor.postProcessPropertyValues()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.populateBean()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.doCreateBean()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.createBean()
at
org.springframework.beans.factory.support.AbstractBeanFactory$
1.getObject()
at
org.springframework.beans.factory.support.DefaultSingletonBean Registry.getSingleton() at
org.springframework.beans.factory.support.AbstractBeanFactory. doGetBean()
at
org.springframework.beans.factory.support.AbstractBeanFactory.
getBean()
at
org.springframework.beans.factory.support.DefaultListableBeanF actory.preInstantiateSingletons()
at
org.springframework.context.support.AbstractApplicationContext .finishBeanFactoryInitialization()
at
org.springframework.context.support.AbstractApplicationContext .refresh()
at
org.springframework.context.support.ClassPathXmlApplicationCon text.()
at
org.springframework.context.support.ClassPathXmlApplicationCon text.()
atcom.spring.test.SpringTest.main() Caused by: : Error creating bean with name 'sessionFactory' defined in class path resource [beans.xml]: Error setting property values; nested exception is : Invalid property 'annotatedClasses' of bean class
[org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Bean property 'annotatedClasses' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.applyPropertyValues()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.populateBean()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.doCreateBean()
at
org.springframework.beans.factory.support.AbstractAutowireCapa
bleBeanFactory.createBean()
at
org.springframework.beans.factory.support.AbstractBeanFactory$
1.getObject()
at
org.springframework.beans.factory.support.DefaultSingletonBean Registry.getSingleton() at
org.springframework.beans.factory.support.AbstractBeanFactory. doGetBean()
at
org.springframework.beans.factory.support.AbstractBeanFactory. getBean()
at
org.springframework.context.annotation.CommonAnnotationBeanPos tProcessor.autowireResource()
at
org.springframework.context.annotation.CommonAnnotationBeanPos tProcessor.getResource()
at
org.springframework.context.annotation.CommonAnnotationBeanPos tProcessor$ResourceElement.getResourceToInject()
at
org.springframework.beans.factory.annotation.InjectionMetadata $InjectedElement.inject()
at
org.springframework.beans.factory.annotation.InjectionMetadata .inject()
at
org.springframework.context.annotation.CommonAnnotationBeanPos tProcessor.postProcessPropertyValues()
... 13 more
Caused by: : Invalid property 'annotatedClasses' of bean class
[org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Bean property 'annotatedClasses' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at
org.springframework.beans.BeanWrapperImpl.setPropertyValue()
at
org.springframework.beans.BeanWrapperImpl.setPropertyValue()
at
org.springframework.beans.AbstractPropertyAccessor.setProperty Values()
at
org.springframework.beans.AbstractPropertyAccessor.setProperty Values()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.applyPropertyValues()
... 26 more
原因是,在选择Session 工程类时,选择的工厂类与PO 映射方式不一致,Hibernate 有两个Session 工厂:
1、LocalSessionFactoryBean :通过XX.hbm.xml 来映射实体类
2、AnnotationSessionFactoryBean :通过注释来映射实体类
选择第一种做Session 工厂时,主要通过以下方式添加PO
选择第二种做Session 工厂时,主要通过以下方式添加PO
com.test.entity.TPerson
2、在往数据库中插入数据时出现以下错误:
Exception in thread "main"
: could not insert:
[com.spring.model.User]
at
org.hibernate.exception.SQLStateConverter.handledNonSpecificEx ception()
at
org.hibernate.exception.SQLStateConverter.convert()
at
org.hibernate.exception.JDBCExceptionHelper.convert()
at
org.hibernate.id.insert.AbstractReturningDelegate.performInser t()
at
org.hibernate.persister.entity.AbstractEntityPersister.insert()
atorg.hibernate.persister.entity.AbstractEntityPersister.in sert()
at
org.hibernate.action.EntityIdentityInsertAction.execute()
atorg.hibernate.engine.ActionQueue.execute()
at
org.hibernate.event.def.AbstractSaveEventListener.performSaveO rReplicate()
at
org.hibernate.event.def.AbstractSaveEventListener.performSave()
at
org.hibernate.event.def.AbstractSaveEventListener.saveWithGene ratedId()
at
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveW ithGeneratedOrRequestedId()
at
org.hibernate.event.def.DefaultSaveEventListener.saveWithGener atedOrRequestedId()
at
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entit yIsTransient() at
org.hibernate.event.def.DefaultSaveEventListener.performSaveOr Update()
at
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSav eOrUpdate()
atorg.hibernate.impl.SessionImpl.fireSave()
atorg.hibernate.impl.SessionImpl.save() atorg.hibernate.impl.SessionImpl.save() atcom.spring.DAOImpl.UserDAOImpl.saveUser()
atcom.spring.service.UserService.saveUserDAO()
atcom.spring.test.SpringTest.main()
Caused by: : Field 'id' doesn't have a default value
atcom.mysql.jdbc.SQLError.createSQLException()
atcom.mysql.jdbc.MysqlIO.checkErrorPacket()
atcom.mysql.jdbc.MysqlIO.checkErrorPacket()
atcom.mysql.jdbc.MysqlIO.sendCommand() atcom.mysql.jdbc.MysqlIO.sqlQueryDirect() atcom.mysql.jdbc.ConnectionImpl.execSQL()
at
com.mysql.jdbc.PreparedStatement.executeInternal()
at
com.mysql.jdbc.PreparedStatement.executeUpdate()
at
com.mysql.jdbc.PreparedStatement.executeUpdate()
at
com.mysql.jdbc.PreparedStatement.executeUpdate()
at
org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpd ate()
at
org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpd ate()
at
org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract()
at
org.hibernate.id.insert.AbstractReturningDelegate.performInser t()
... 18 more
原因是在代码中设置了主键自动增长,而在数据库表中并没有设置主键列自增
3、在spring 与hibernate 集成时出现下列错误
Exception in thread "main" : Error creating bean with name 'userService' defined in file
[E:\software\Myeclipse
10.6\workspace\Spring-11Spring_Hibernate_Transaction\bin\com\spring\service\UserService.class]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError:
org/objectweb/asm/Type
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.doCreateBean()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.createBean()
at
org.springframework.beans.factory.support.AbstractBeanFactory$
1.getObject()
at
org.springframework.beans.factory.support.DefaultSingletonBean Registry.getSingleton() at
org.springframework.beans.factory.support.AbstractBeanFactory. doGetBean()
at
org.springframework.beans.factory.support.AbstractBeanFactory. getBean()
at
org.springframework.beans.factory.support.DefaultListableBeanF actory.preInstantiateSingletons()
at
org.springframework.context.support.AbstractApplicationContext .finishBeanFactoryInitialization()
at
org.springframework.context.support.AbstractApplicationContext .refresh()
at
org.springframework.context.support.ClassPathXmlApplicationCon text.()
at
org.springframework.context.support.ClassPathXmlApplicationCon text.()
atcom.spring.test.SpringTest.main() Caused by: java.lang.NoClassDefFoundError:
org/objectweb/asm/Type
atnet.sf.cglib.core.TypeUtils.parseType() atnet.sf.cglib.core.KeyFactory.() atnet.sf.cglib.proxy.Enhancer.() at
org.springframework.aop.framework.Cglib2AopProxy.createEnhance
r()
at
org.springframework.aop.framework.Cglib2AopProxy.getProxy()
at
org.springframework.aop.framework.ProxyFactory.getProxy()
at
org.springframework.aop.framework.autoproxy.AbstractAutoProxyC reator.createProxy()
at
org.springframework.aop.framework.autoproxy.AbstractAutoProxyC reator.wrapIfNecessary() at
org.springframework.aop.framework.autoproxy.AbstractAutoProxyC reator.postProcessAfterInitialization()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.applyBeanPostProcessorsAfterInitialization()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.initializeBean()
at
org.springframework.beans.factory.support.AbstractAutowireCapa bleBeanFactory.doCreateBean()
... 11 more
Caused by: :
org.objectweb.asm.Type
at java.net.URLClassLoader$1.run() atjava.security.AccessController.doPrivileged() atjava.net.URLClassLoader.findClass()
atjava.lang.ClassLoader.loadClass() atsun.misc.Launcher$AppClassLoader.loadClass()
atjava.lang.ClassLoader.loadClass() atjava.lang.ClassLoader.loadClassInternal()
... 23 more
解决方法:
把Hibernate 的cglib.jar 换成spring 的
cglib.jar(com.springsource.net.sf.cglib-2.2.0.jar)