整合Spring与Hibernate 异常 Unable to instantiate default tuplizer

在测试《Spring3.x企业级实战》第10章有关事务的例子(HibernateSpring整合)时,碰到如下异常 Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/baobaotao/withouttx/hiber/hiberWithoutTx.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1482)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:610)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at com.baobaotao.withouttx.hiber.UserHibernateWithoutTransManagerService.main(UserHibernateWithoutTransManagerService.java:38)
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:108)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:133)
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.(EntityEntityModeToTuplizerMapping.java:80)
at org.hibernate.tuple.entity.EntityMetamodel.(EntityMetamodel.java:322)
at org.hibernate.persister.entity.AbstractEntityPersister.(AbstractEntityPersister.java:485)
at org.hibernate.persister.entity.SingleTableEntityPersister.(SingleTableEntityPersister.java:133)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
at org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:286)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:805)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:745)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:134)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
... 12 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:506)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:105)
... 25 more
Caused by: java.lang.NoClassDefFoundError: javassist/util/proxy/MethodFilter
at org.hibernate.bytecode.javassist.BytecodeProviderImpl.getProxyFactoryFactory(BytecodeProviderImpl.java:49)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactoryInternal(PojoEntityTuplizer.java:205)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:183)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.(AbstractEntityTuplizer.java:167)
at org.hibernate.tuple.entity.PojoEntityTuplizer.(PojoEntityTuplizer.java:77)
... 30 more

从异常的造成原因Caused by 可以看到!

一共有三个造错误的原因

Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer

Caused by: java.lang.reflect.InvocationTargetException

Caused by: java.lang.NoClassDefFoundError: javassist/util/proxy/MethodFilter

从网上查了一下造成Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer原因可以分为以下几个

第一种:

        可能是缺少javassist.jar包,下面给出下载地址

http://sourceforge.net/projects/jboss/files/Javassist/3.18.0-GA/ 

一定要确保版本对应不然还是会有错,最好在官网上下,我之前在百度下好把它加入Referenced Library 结果还是报错,后来才发现javassist.jar下面没有avassist/util/proxy/MethodFilter,没有要的那个类。很多时候版本不一样,造成jar包下面没有我们要的类,经常会因此出错!最好用Maven,我也准备用这个了,有点落伍了,呵呵!

第二种:

   可能是hibernate.cfg.xmlmapping配置错误,如果用注释的话一下要保证注释的实体(JavaBean)正确!确保实体表或映射表中属性写的一致导致的。所以要细心检查实体类、.hbm.xml和数据库之间的属性名、字段名还有类型是否写错了。

第三种:

   可能是某个实体类中外加了某些getXXX(),而不存在XX属性,这时要在get上加@Transient,不持久化它就行了。

利用MyEclipse自动生成 setXXX() 和getXXX()时,一定要注意Boolean 类型成量变量,因为MyEclipse不会为Boolean类型变量生成setXXX() 和getXXX()方法,而是

public Boolean isInvalid() {
    return isInvalid;
 }
public void isInvalid(Boolean isInvalid) {
  this.isInvalid = isInvalid;
 }

一定要确保把它改成setXXX()和getXXX(),不然会报错!
第三个Caused by:    java.lang.NoClassDefFoundError: javassist/util/proxy/MethodFilter
   NoClassDefFoundError(类定义未找到错误) 告诉我们未定义, 一般这个异常的都是由于没有加入相关的jar包造成的,如果加入相关jar还有这个,看一下jar包下没有Caused by下面对应的类(javassist/util/proxy/MethodFilter
当我在官网上下载 javassist.jar 包并加入时,错误解决!
如果你也碰到Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer异常先看Caused by后面的内容, 然后 判断是上面三种可能造成Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer的哪一种,问题一定能解决!



 


你可能感兴趣的:(Spring,Java,EE,Hibernate)