Genric -validation-constraints-violation-while-execution-automatic-bean-validation

时间:2015-05-15 16:12:19

标签: java spring jpa eclipselink

我想为返回的所有不同实体编写一个通用方法 消息列表。

Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'

例如:伪代码

enter code here
private List<String> constraintViolationsDetected (genericentity) {
    Set<ConstraintViolation<?>> constraintViolations = validator.validate(?);
}

我正在使用javax.persistence [eclipselink-2.6.0]&amp;斯普林斯

这是我的代码,我想在保存到数据库之前验证DeploymentConfiguration Entity。我还有3个不同的实体 - 我还必须为其他实体编写验证。 注意:最后,我将调用这些方法并作为Rest Services(Jersy)公开

1)DeploymentConfiguration是模型/实体

package com.Demo.persistence.model;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

      public class DeploymentConfiguration implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name= DBConstants.DEPLOYMENT_ID,length = 255)
    @NotNull(message="DepoymentId type must be specified.")
    @Size(min=1, max=255)
    //@Valid
    private String depoymentId;

    @Basic
    @Column(name = DBConstants.SERVER_TYPE_VALUE, length = 255) 
    @NotNull(message="Deployment Server must be specified.")
    @Size(min=1, max=255)
    @Pattern(regexp="^(Dev|QA|Production)$",message="Invalid Servertype
private String deploymentServerValue;

     public String getDepoymentId () {
        return depoymentId;
    }

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

    public String getDeploymentServerValue () {
        return deploymentServerValue;
    }

    public void setDeploymentServerValue (String deploymentServerValue) 
{
        this.deploymentServerValue = deploymentServerValue;
}

2)DaoImplementation

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
public class DeploymentConfigurationDaoImpl 
{
EntityManagerProvider entityManagerProvider;
public void saveConnectionConfigurations(ConnectionConfiguration connConfig) {
        LOG.debug("Saving depoyment Configuration");
        if (null == connConfig) {
            return;
        }
        EntityManager em = null;
        em = getEntityManagerProvider().getEntityManager();
        try {
            em.getTransaction().begin();

                em.persist(connConfig);
                em.getTransaction().commit();
                em.getEntityManagerFactory().getCache().evictAll();
                LOG.debug("Persisting data");

        } 
catch (Exception ex) 
{
LOG.error("Error while persisting the depoyment Configuration", ex);

} finally {
    if (em != null && em.isOpen())
    em.close();

}

}

---------------- 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.DeploymentConfiguration</class>
        <shared-cache-mode>ALL</shared-cache-mode>
        <properties>
            <property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
        </properties>
    </persistence-unit>
</persistence>

----------------------------------------------- ---------------------------------测试persistence.xml - 与您分享

enter code here

<?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.DeploymentConfiguration </class>

        <shared-cache-mode>ALL</shared-cache-mode>
        <properties>
            <!-- Connection Properties For Test DB -->
            <property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:target/CoreServicesTestDB;create=true" />
            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
            <property name="javax.persistence.jdbc.user" value="gomobile" />
            <property name="javax.persistence.jdbc.password" value="secret" />
            <property name="eclipselink.target-database" value="Derby" />
            <property name="eclipselink.target-server" value="None" />
            <!-- General Properties -->
            <property name="eclipselink.cache.shared.default" value="false" />
            <property name="eclipselink.weaving" value="static" />
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
            <property name="eclipselink.ddl-generation.output-mode"
                value="both" />
            <!-- Logging Properties -->
            <property name="eclipselink.logging.logger" value="DefaultLogger" />
            <property name="eclipselink.logging.level" value="FINEST" />
            <property name="eclipselink.logging.level.sql" value="FINEST" />
            <property name="eclipselink.logging.level.cache" value="FINEST" />
        </properties>
    </persistence-unit>
</persistence>

我正在使用以下依赖项

<!-- JPA dependencies - Start -->
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
        </dependency>
        <!-- JPA dependencies - Start -->

        <!-- Spring dependencies - Start -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <!-- Spring dependencies - End -->
        <!-- slf4j dependencies - Start -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>com.springsource.slf4j.log4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>com.springsource.slf4j.api</artifactId>
        </dependency>
        <!-- slf4j dependencies - End -->
        <!-- Junits dependencies - Start -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.orm</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.servicemix.bundles</groupId>
            <artifactId>org.apache.servicemix.bundles.derby</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Junits dependencies - End -->

0 个答案:

没有答案