Unable to get the default Bean Validation

原文地址:Unable to get the default Bean Validation  作者:Tuikeer

   按照马士兵的ssh整合视频开始配置ssh架构,但是由于版本不相近遇到了两个问题,特此记录下:

 

1.java.lang.NoSuchFieldError:INSTANCEatorg.hibernate.type.BasicTypeRegistry.(BasicTypeRegistry.java:94)atorg.hibernate.type.TypeResolver.(TypeResolver.java:59)atorg.hibernate.cfg.Configuration.(Configuration.java:249)atorg.hibernate.cfg.Configuration.(Configuration.java:300)

 

 

 

经过查找发现是hibernate-annotations.jar和hibernate-commons-annotations.jar的问题,hibernate.jar已经将其整合

 

 

2.去掉两个包之后出现

 

Error creating bean with name 'userManager': Injection of autowireddependencies failed; nested exception isorg.springframework.beans.factory.BeanCreationException: Could notautowire field: private com.bolo.examples.dao.base.UserDaocom.bolo.examples.service.base.UserManager.userDao; nestedexception isorg.springframework.beans.factory.BeanCreationException: Errorcreating bean with name 'userDao' defined in file[E:MyEclipse9.0workspacessh2WebRootWEB-INFclassescomboloexamplesdaobaseUserDao.class]:Initialization of bean failed; nested exception isorg.springframework.beans.factory.BeanCreationException: Errorcreating bean with name 'sessionFactory' defined in class pathresource [applicationContext.xml]: Invocation of init methodfailed; nested exception is org.hibernate.HibernateException:Unable to get the default Bean Validation factory

 

也是一番查找之后发现在

 

<beanid="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <propertyname="dataSource" ref="dataSource" />
  <propertyname="hibernateProperties">
   <props>
         <propkey="hibernate.show_sql">true</prop>
        <propkey="hibernate.format_sql">true</prop>
        <propkey="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <propkey="javax.persistence.validation.mode">none</prop>    

 </props>

  </property>
  <propertyname="packagesToScan" value="com.bolo.examples.entity.*"/>
 </bean>

 

加入红色部分就没有问题了

真是不同的版本差异比较大啊

 

其实这个问题是我们自己造成的!为什么这么说?因为我们在配置Spring和Hibernate进行结合的时候版本出现了问题。
<persistence...>  
  <persistence-unit...> 
   ... 
   <properties> 
     <propertyname="javax.persistence.validation.mode" 
               value="callback, ddl"/> 
   </properties> 
 </persistence-unit> 
</persistence> 
这是hibernate官方文档的一段话!

意思就是在hibernate.cfg.xml或者是
persistence.xml文件下面需要配置
javax.persistence.validation.mode属性!

特别的!在Hibernate中默认的

<propkey="javax.persistence.validation.mode">none</prop>
是auto而不是none!

 

------------------------------------------------------------------------------------------------------------------------------------------

 

javax.persistence.validation.mode默认情况下是auto的,就是说如果不设置的话它是会自动去你的classpath下面找一个bean-validation**包,但是找不到,所以beanvalitionFactory错误。
由于javax.persistence.validation.mode的属性值默认是auto,所以会出错。
 
在hibernate.cfg.xml里将javax.persistence.validation.mode设置为none,就可以避免出错了。
 
  <!-- Disable the BeanValidation-->
  <propertyname="javax.persistence.validation.mode">none</property>

------------------------------------------------------------------------------------------------------------------------------------------

 

 

所以,Hibernate3.6以上版本在用junit测试时会提示错误:

 

Unable to get the default Bean Validationfactory

 

在hibernate.cfg.xml里增加一属性解决:    

 

<propertyname="javax.persistence.validation.mode">none</property>

你可能感兴趣的:(Unable to get the default Bean Validation)