使用hibernate时出现 org.hibernate.HibernateException: Unable to get the default Bean Validation factory...

hibernate 在使用junit测试报错:

org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    at org.hibernate.cfg.beanvalidation.BeanValidationActivator.applyDDL(BeanValidationActivator.java:127)
    at org.hibernate.cfg.Configuration.applyBeanValidationConstraintsOnDDL(Configuration.java:1704)
    at org.hibernate.cfg.Configuration.applyConstraintsToDDL(Configuration.java:1654)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1445)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856)
    at cn.ustc.test.HibernateTest1.testInsert(HibernateTest1.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.hibernate.cfg.beanvalidation.BeanValidationActivator.applyDDL(BeanValidationActivator.java:118)
    ... 28 more
Caused by: org.hibernate.HibernateException: Unable to build the default ValidatorFactory
    at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:383)
    at org.hibernate.cfg.beanvalidation.TypeSafeActivator.applyDDL(TypeSafeActivator.java:109)
    ... 33 more
Caused by: javax.validation.ValidationException: Unable to instantiate Configuration.
    at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:272)
    at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)
    at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:380)
    ... 34 more
Caused by: java.lang.NullPointerException
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:960)
    at org.hibernate.validator.engine.ResourceBundleMessageInterpolator.loadBundle(ResourceBundleMessageInterpolator.java:202)
    at org.hibernate.validator.engine.ResourceBundleMessageInterpolator.getFileBasedResourceBundle(ResourceBundleMessageInterpolator.java:182)
    at org.hibernate.validator.engine.ResourceBundleMessageInterpolator.<init>(ResourceBundleMessageInterpolator.java:81)
    at org.hibernate.validator.engine.ResourceBundleMessageInterpolator.<init>(ResourceBundleMessageInterpolator.java:73)
    at org.hibernate.validator.engine.ConfigurationImpl.<init>(ConfigurationImpl.java:57)
    at org.hibernate.validator.HibernateValidator.createGenericConfiguration(HibernateValidator.java:43)
    at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:269)
    ... 36 more

解决方案:

hibernate.cfg.xml中配置的时候  增加如下配置

<property name="javax.persistence.validation.mode">none</property>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <!-- 配置会话工厂(hibernate核心对象,管理数据库连接池) -->
    <session-factory>
        <!-- 连接数据库 JDBC 四个基本连接参数 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">111</property>
        
        <property name="javax.persistence.validation.mode">none</property>
        
        <!-- 配置hibernate 使用 数据库方言 -->
        <!-- 方言解决 不同数据库 之间区别 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        
        <!-- 其它属性 -->
        <!-- 自动建表 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 在日志中输出SQL -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        
        <!-- 事务默认操作 -->
        <property name="hibernate.connection.autocommit">false</property>
        
        <!-- 配置c3p0 -->
        <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        
        <!-- 加载hbm映射 -->
        <mapping resource="cn/ustc/domain/Customer.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

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

转载于:https://www.cnblogs.com/liuleicode/p/5041667.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值