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 further details.
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.itheima.utils.Test.main(Test.java:5)
Caused by: 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 com.itheima.utils.HibernateUtils.<clinit>(HibernateUtils.java:19)
    ... 1 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.hibernate.cfg.beanvalidation.BeanValidationActivator.applyDDL(BeanValidationActivator.java:118)
    ... 6 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)
    ... 11 more
Caused by: javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
    at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:271)
    at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:110)
    at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:380)
    ... 12 more

我们先看下hibernate的帮助文档,该文档所在位置:\hibernate-distribution-3.6.10.Final\documentation\manual\zh-CN\html ,可以看中文版本的,有如下描述,截图如下:

    

这里大意就是在 hibernate.cfg.xml 或者是 persistence.xml 文件下面需要配置 javax.persistence.validation.mode 属性,

于是我们在 hibernate.cfg.xml 文件里添加属性,如下截图所示:

  

 

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

添加配置后,不报错了。javax.persistence.validation.mode 默认情况下是auto的,就是说如果不设置的话它是会自动去你的classpath下面找一个bean-validation包,如果没有导入hibernate-validator相关的两个jar包(官网有下载),所以 beanvalitionFactory错误。

posted @ 2018-07-15 19:22  黑泽君  阅读(520)  评论(0编辑  收藏  举报