首页 > 代码库 > spring和hibernate整合时报sessionFactory无法获取默认Bean Validation factory

spring和hibernate整合时报sessionFactory无法获取默认Bean Validation factory

Hibernate 3.6以上版本在用junit测试时会提示错误: Unable to get the default Bean Validation factory

spring和hibernate整合时报sessionFactory无法获取默认Bean Validation factory  ,是因为新版hibernate用到新的jar包造成的,默认会自动找验证包,吴国不需要这一步,可以在spring整合hibernate的配置节点中添加如下标红属性:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
<property name="mappingLocations" value="http://www.mamicode.com/classpath:cb/model/Custorm.hbm.xml"></property>
</bean>

原因是:在Hibernate中默认的 <prop key="javax.persistence.validation.mode">none</prop> 是auto而不是none!

javax.persistence.validation.mode默认情况下是auto的,就是说如果不设置的话它是会自动去找一个bean-validation.jar,但是找不到,所以报beanvalitionFactory错误。

如果想用验证包,可以添加这些jar包

<dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0</version>
    </dependency>

spring和hibernate整合时报sessionFactory无法获取默认Bean Validation factory