您的位置:首页 > 编程语言 > Java开发

Unable to get the default Bean Validation factory

2013-11-23 22:30 393 查看
这两天在整合SSH框架,遇到不少问题其中一个:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException:
Unable to get the default Bean Validation factory

下面解决方法为"Nemo的空间"里的方法,成功解决问题.

 

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

  <property name="dataSource" ref="dataSource" />

  <property name="hibernateProperties">

   <props>

         <prop key="hibernate.show_sql">true</prop>

         <prop key="hibernate.format_sql">true</prop>

         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

         <prop key="javax.persistence.validation.mode">none</prop>    

  </props>

  </property>

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

 </bean>

 

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

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

 

其实这个问题是我们自己造成的!为什么这么说?因为我们在配置Spring和Hibernate进行结合的时候版本出现了问题。

<persistence ...>  

  <persistence-unit ...> 

    ... 

    <properties> 

      <property name="javax.persistence.validation.mode" 

                value="callback, ddl"/> 

    </properties> 

  </persistence-unit> 

</persistence> 
这是hibernate官方文档的一段话!

意思就是在hibernate.cfg.xml或者是

persistence.xml文件下面需要配置

javax.persistence.validation.mode属性!

特别的!在Hibernate中默认的

<prop key="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 -->

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

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

 

 

所以,Hib
4000
ernate 3.6以上版本在用junit测试时会提示错误:

 

Unable to get the default Bean Validation factory

 

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

 

<property name="javax.persistence.validation.mode">none</property>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  框架 测试 junit spring bean
相关文章推荐