使用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错误。

时间: 2024-10-13 04:04:58

使用hibernate时出现 org.hibernate.HibernateException: Unable to get the default Bean Validation factory的相关文章

hibernate 错误:org.hibernate.HibernateException: Unable to get the default Bean Validation factory

最近在自学Hibernate与Struts2进行集成时,出现了一个错误,错误截图如下: 错误代码如下: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for

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="sessio

Unable to build the default ValidatorFactory

今天数据库连接配置全写在hibernate.cfg.xml的时候没有问题,与spring整合的时候就出现了这个问题,反复查了代码,没找到问题 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init

Caused by:org.hibernate.HibernateException:Unable to make JDBC Connection

1.错误描述 Caused by:org.hibernate.HibernateException:Unable to make JDBC Connection[jdbc\:mysql\://localhost\:3306/school] 2.错误原因 在hibernate.cfg.xml中配置数据库路径时 <property name="connection.url"> jdbc\:mysql://localhost\:3306/school </property&

org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]

使用Hibernate 插入List数据时出现了以下异常: 1 SLF4J: The requested version 1.6 by your slf4j binding is not compatible with [1.5.5, 1.5.6, 1.5.7, 1.5.8] 2 SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details. 3 log4j:WARN No appenders co

Spring整合HIbernate时,三种数据库连接池的配置和比较

现在常用的开源数据库连接池主要有c3p0.dbcp.proxool三种,其中: Spring                         推荐使用dbcp: Hibernate                  推荐使用c3p0和proxool: 1.  DBCP:Apache DBCP(DataBase connection pool)数据库连接池.是Apache上的一个 java连接池项目,也是 tomcat使用的连接池组件.单独使用dbcp需要3个包:common-dbcp.jar,c

【Hibernate学习笔记-3】在Spring下整合Hibernate时, 关于sessionFactory的类型的说明

摘要 在Spring下整合Hibernate时,关于sessionFactory的配置方式主要有两种,分别为注解配置方式,和xml配置方式,下面将对这两种配置方式进行介绍. 1. sessionFactory和数据库对应,有多少个数据库,就需要配置多少个sessionFactory: 2. session相当于数据库连接,进行数据库的CRUD操作时,需要开启session,用完需要关闭session: 3. 配置sessionFactory,主要要配置如下三个方面: 3.1. 数据源配置: 3.

Spring 整合 Hibernate 时启用二级缓存实例详解

写在前面: 1. 本例使用 Hibernate3 + Spring3: 2. 本例的查询使用了 HibernateTemplate: 1. 导入 ehcache-x.x.x.jar 包: 2. 在 applicationContext.xml 文件中找到 sessionFactory 相应的配置信息并在设置 hibernateProperties 中添加如下代码: ? 1 2 3 4 5 6 <!-- 配置使用查询缓存 --> <prop key="hibernate.cach

攻城狮在路上(壹) Hibernate(九)--- Hibernate的映射类型

Hibernate采用映射类型作为Java类型和SQL类型的桥梁,对应type属性.分为两种:内置映射类型和客户化映射类型.一.内置映射类型: 1.Java基本类型的Hibernate映射类型: Java基础类型的Hibernate映射类型 Hibernate映射类型 Java类型 标准SQL类型 大小和取值范围 integer或者int int或者java.lang.Integer INTEGER   long long BIGINT   short short SMALLINT   byte