这两天在整合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>
------------------------------------------------------------------------------------------------------------------------------------------
所以,Hibernate 3.6以上版本在用junit测试时会提示错误:
Unable to get the default Bean Validation factory
在hibernate.cfg.xml里增加一属性解决:
<property name="javax.persistence.validation.mode">none</property>