javax.validation.ValidationException:HV000041:调用TraversableResolver.isReachable()引发异常

时间:2015-05-19 18:49:22

标签: java jpa eclipselink

我得到以下错误

  

javax.validation.ValidationException:HV000041:调用   TraversableResolver.isReachable()引发了异常。在   org.hibernate.validator.internal.engine.ValidatorImpl.isReachable(ValidatorImpl.java:1405)     在

     

org.hibernate.validator.internal.engine.ValidatorImpl.isValidationRequired(ValidatorImpl.java:1381)

     

在   org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:542)     在   org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:487)     在   org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:451)   在   org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:403)     在   org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:206)     在   com.demo.AdminValidator.constraintViolationsForConnMapping(AdminValidator.java:75)     在   com.demo.utils.tests.AdminValidatorTest.test1ConnectionIdIsRequired(AdminValidatorTest.java:121)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:606)at   org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)     在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在   org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)     在   org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)     在   org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)     在   org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)     在   org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)     在   org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)     在   org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)at at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at at   org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)     在   org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)     在org.junit.runners.ParentRunner.run(ParentRunner.java:363)at   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)     在   org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)     在   org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)   引起:java.lang.NullPointerException at   com.dem.model.DeploymentConfigurationMappingPK.hashCode(DeploymentConfigurationMappingPK.java:79)     在   org.hibernate.validator.internal.engine.resolver.CachingTraversableResolverForSingleValidation $ TraversableHolder.buildHashCode(CachingTraversableResolverForSingleValidation.java:153)     在   org.hibernate.validator.internal.engine.resolver.CachingTraversableResolverForSingleValidation $ TraversableHolder。(CachingTraversableResolverForSingleValidation.java:114)     在   org.hibernate.validator.internal.engine.resolver.CachingTraversableResolverForSingleValidation $ TraversableHolder。(CachingTraversableResolverForSingleValidation.java:96)     在   org.hibernate.validator.internal.engine.resolver.CachingTraversableResolverForSingleValidation.isReachable(CachingTraversableResolverForSingleValidation.java:41)     在   org.hibernate.validator.internal.engine.ValidatorImpl.isReachable(ValidatorImpl.java:1396

这是我的代码 ----------------------- Junit ----------- // DeploymentConfigurationMappingPK在construtor中完成的初始化

@Test
    public void test1ConnectionIdIsRequired() 
    {

        connectionMappingPK.setConnectionId(null);      
        List<String> errorMessages=null;                
        errorMessages=adminValidator.constraintViolationsForDeploymentConfigMapping(connectionMappingPK);

        Assert.assertTrue(errorMessages.contains("ConnectionId must be specified."));
    }

DeploymentConfigurationMappingPK.java

import java.io.Serializable;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.demo.DBConstants;

@Embeddable
public class DeploymentConfigurationMappingPK implements Serializable {

    private static final long serialVersionUID = 1L;

    @JoinColumn(name = DBConstants.CONNECTION_ID, nullable = false)
    @Column(name = DBConstants.CONNECTION_ID, nullable = false, length = 255)   
    @NotNull(message="ConnectionId must be specified.")
    @Size(min=1, max=255)
    private String connectionId;

    @Basic
    @Column(name = DBConstants.APPLICATION_ID, length = 255)
    @NotNull(message="ApplicationId must be specified.")
    @Size(min=1, max=255)
    private String applicationId;

    @Basic
    @Column(name = DBConstants.APPLICATION_VERSION, length = 10)
    @NotNull(message="ApplicationVersion must be specified.")
    @Size(min=1, max=10)
    private String applicationVersion;

    public String getConnectionId() {
        return connectionId;
    }

    public void setConnectionId(String connectionId) {
        this.connectionId = connectionId;
    }

    public String getApplicationId() {
        return applicationId;
    }

    public void setApplicationId(String applicationId) {
        this.applicationId = applicationId;
    }

    public String getApplicationVersion() {
        return applicationVersion;
    }

    public void setApplicationVersion(String applicationVersion) {
        this.applicationVersion = applicationVersion;
    }

    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof ConnectionMappingPK)) {
            return false;
        }
        ConnectionMappingPK castOther = (ConnectionMappingPK) other;
        return this.connectionId.equals(castOther.getConnectionId())
                && this.applicationId.equals(castOther.getApplicationId())
                && this.applicationVersion.equals(castOther
                        .getApplicationVersion());

    }

    public int hashCode() {
        final int prime = 31;
        int hash = 17;
        hash = hash * prime + this.connectionId.hashCode();
        hash = hash * prime + this.applicationVersion.hashCode();
        hash = hash * prime + this.applicationId.hashCode();
        return hash;

    }

}

----------------------------------------- AdminValidator ----- -------------------------------------------------- --------------------------------------------------

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.Demo.persistence.model.DeploymentConfigurationMappingPK ;

public class AdminValidator {

    private static final Logger LOG = LoggerFactory
            .getLogger(AdminValidator.class);
    private ValidatorFactory factory = null;
    private Validator validator = null;

    public AdminValidator() {
        if (factory == null) {
            factory = Validation.buildDefaultValidatorFactory();
            validator = factory.getValidator();
        }
    }




    public List<String> constraintViolationsForDeploymentConfigMapping(DeploymentConfigurationMappingPK connMapPK) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("In handler #constraintViolationsDetectedForConnMapping of AdminValidator");
        }
        List<String> errorMessages = new ArrayList<String>();
        **Set<ConstraintViolation<DeploymentConfigurationMappingPK>> constraintViolations = validator**
                .validate(connMapPK);
        if (constraintViolations.size() > 0)
        {
            Iterator<ConstraintViolation<DeploymentConfigurationMappingPK>> iterator = constraintViolations
                    .iterator();
            while (iterator.hasNext())
           {
                ConstraintViolation<DeploymentConfigurationMappingPK> cv = iterator
                        .next();

                errorMessages.add(cv.getMessage());
            }

        } 
        return errorMessages;
    }
}

---------------- Persistence .xml ---------------------------- ----------

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="com.Demo.persistence" 
        transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>       
        <class>com.Demo.persistence.model.DeploymentConfigurationMappingPK</class>
        <shared-cache-mode>ALL</shared-cache-mode>
        <properties>
            <property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
        </properties>
    </persistence-unit>
</persistence>

我们在演示项目中使用了eclipselink,springframework,我们没有使用Spring MVC。

3 个答案:

答案 0 :(得分:16)

堆栈跟踪显示您的hashcode方法正在抛出NPE:

Caused by: java.lang.NullPointerException at com.dem.model.DeploymentConfigurationMappingPK.hashCode(DeploymentConfigurationMappingPK.java:79) at 

您需要修复hashCode方法来处理null。

答案 1 :(得分:0)

似乎与bug HV-1013有关,已在hibernate 5.3.0.CR1中修复。

答案 2 :(得分:0)

对于任何在Websphere 8.5中面临此问题的人。如果您已经有EntityManager实例,请遵循此步骤。我必须使用JPA 2.1,因此启用了Classes loaded with local class loader first (parent last) 如此处所示-Class Loader Setting

我的设置-

Java 7

Hibernate Core : 5.1.17.Final

Hibernate-validator : 4.2.0.Final

  1. 创建自定义TraversableResolver

<script src="https://gist.github.com/abitgen/21bc364944cb52c23b2dfbba9e137133.js"></script>

  1. 使用上面的Validator创建一个TraversableResolver对象,这里我返回一个ConstraintViolation的字符串。

<script src="https://gist.github.com/abitgen/a0c248e68c5b2180ebbc70c1450d10a5.js"></script>

  1. 要验证任何实体,请调用validateEntity方法。

这是我的pom.xml

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <org.hibernate.core>5.1.17.Final</org.hibernate.core>
    <org.hibernate.validator>4.2.0.Final</org.hibernate.validator>

    </properties>

 <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${org.hibernate.core}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>${org.hibernate.validator}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator-annotation-processor -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator-annotation-processor</artifactId>
        <version>${org.hibernate.validator}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${org.hibernate.core}</version>
    </dependency>