Unable to get the default Bean Validation

来源:互联网 发布:网络电视老是卡怎么办 编辑:程序博客网 时间:2024/05/21 02:34

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

 

1.java.lang.NoSuchFieldError: INSTANCEat org.hibernate.type.BasicTypeRegistry.(BasicTypeRegistry.java:94)at org.hibernate.type.TypeResolver.(TypeResolver.java:59)at org.hibernate.cfg.Configuration.(Configuration.java:249)at org.hibernate.cfg.Configuration.(Configuration.java:300)

 

 

 

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

 

 

2.去掉两个包之后出现

 

Error creating bean with name 'userManager': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.bolo.examples.dao.base.UserDao com.bolo.examples.service.base.UserManager.userDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in file [E:\MyEclipse9.0\workspace\ssh2\WebRoot\WEB-INF\classes\com\bolo\examples\dao\base\UserDao.class]: Initialization of bean failed; nested exception is 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

 

也是一番查找之后发现在

 

<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>

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

 

 

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

 

Unable to get the default Bean Validation factory

 

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

 

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


源自:http://blog.sina.com.cn/s/blog_6ac4c6cb01018rl2.html

原创粉丝点击