2

得到错误:

org.hibernate.HibernateException: Error applying BeanValidation relational constraints
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.applyRelationalConstraints(BeanValidationIntegrator.java:219)
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:126)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:301)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)


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.BeanValidationIntegrator.applyRelationalConstraints(BeanValidationIntegrator.java:208)
... 207 more
Caused by: org.hibernate.HibernateException: Unable to build the default ValidatorFactory
at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:524)
at org.hibernate.cfg.beanvalidation.TypeSafeActivator.applyDDL(TypeSafeActivator.java:119)
    ... 212 more
Caused by: javax.validation.ValidationException: Unable to find a default provider
at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:264)
at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)
at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:521)

Hibernate 相关的 Maven 配置:

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.1.5.SP1</version>
        <scope>compile</scope>
        <exclusions> 
            <exclusion>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.jtds</groupId>
        <artifactId>jtds</artifactId>
        <version>1.2.4</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
        <scope>compile</scope>
        <exclusions> 
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator-annotation-processor</artifactId>
        <version>4.3.1.Final</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.google.code.findbugs</groupId>
        <artifactId>jsr305</artifactId>
        <version>1.3.9</version>
        <scope>compile</scope>
    </dependency>

我发现的建议是:

  1. 使用休眠 4+
  2. 用户休眠验证 4+
  3. 添加休眠验证库
  4. 添加 jsr305(imo 没关系)
  5. 添加 javax 验证库
  6. 添加休眠验证器注释处理器

我有这些,但仍然出现错误。

编辑:

展示我如何使用我的休眠模式。

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
        <property name="hibernate.connection.pool_size">5</property>
        <property name="show_sql">false</property>
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="hibernate.current_session_context_class">thread</property>


        <mapping class="com.foo.bar.MyClass />
    </session-factory>
</hibernate-configuration>

建造:

private static SessionFactory buildSessionFactory() {

    try {
        // Create the SessionFactory from hibernate.cfg.xml
        Configuration configuration = new Configuration();
        configuration.configure();

        configuration.setNamingStrategy(ImprovedNamingStrategy.INSTANCE); // also other configuration changes made here

        serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();

        return configuration.buildSessionFactory(serviceRegistry); // this line gives the error


    } catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        log.error("Initial SessionFactory creation failed.", ex);
        throw new ExceptionInInitializerError(ex);
    }
}
4

1 回答 1

3

关于你的6点:

  • 使用休眠 4+
  • 用户休眠验证 4+

Hibernate Validator 4.x 是 Bean Validation 的参考实现。这是使用正确的版本。它集成在 Hibernate 4 中。所以版本看起来是正确的

  • 添加休眠验证库

是的,这是必需的,您可以通过添加hibernate-validator依赖项来做到这一点。

  • 添加 jsr305(imo 没关系)

不相关的。甚至不确定那是什么 JSR。

  • 添加 javax 验证库

你明确地这样做,但它们也是对hibernate-validator的传递依赖,所以你可以得到它们。

  • 添加休眠验证器注释处理器

不,不需要/不相关

那么为什么它不起作用呢?我认为要回答这个问题,我们需要更多信息。该异常表明javax.validation.Validation无法查看/加载 Hibernate Validator Bean Validation 提供程序。您是否在受限的课堂加载环境中工作?您如何使用所有这些依赖项?

于 2013-03-17T14:11:51.447 回答