这个问题是该问题的后续解决方案:JPAConstraintViolation与回滚
我对JPA和验证API(JSR-303)的组合进行了一些测试。
我在JPA规范中找到了以下内容(第101-102页):
默认情况下,默认Bean验证组(默认组)将在持久化和更新前生命周期验证事件时进行验证
…
如果validate方法返回的ConstraintViolation对象集不为空,则持久性提供程序必须抛出javax.validation.ConstraintViolationException,其中包含对返回的ConstraintViolation对象集的引用,并且必须将事务标记为回滚。
我设置以下测试:
NameNotNullWithDefaultGeneratedStrategy
,其ID由默认策略(@Generated
)和@NotNull String name
列生成NameNotNullWithTableGeneratedStrategy
,其ID由表格策略(@TableGenerated
)和@NotNull String name
列生成persist
每个实体的一个实例使用null name
。javax.validation.ConstraintViolationException
由persist方法抛出,并且事务标记为rollback only
(即,这些假设基于本文引用的JPA规范)。结果是:
persist
方法javax.validation.ConstraintViolationException
对两个实体都引发一个。rollback only
两种情况下交易均标记为persist
引发一个javax.validation.ConstraintViolationException
for实体NameNotNullWithDefaultGeneratedStrategy
+标记为的交易rollback only
persist
不要为 未标记* 为的实体NameNotNullWithTableGeneratedStrategy
+事务抛出任何异常 *rollback only
commit
因为NameNotNullWithTableGeneratedStrategy
失败RollbackException
问题是:
这是我测试的代码:
package com.example.jpa.validator;
import org.junit.Assert;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.RollbackException;
public class ConstraintViolationExceptionTest {
@Test
public void testHibernateDefaultStrategy() { // Success
testPersistWithNullName("pu-hibernate",new NameNotNullWithDefaultGeneratedStrategy());
}
@Test
public void testHibernateTableStrategy() {
testPersistWithNullName("pu-hibernate",new NameNotNullWithTableGeneratedStrategy());
//this test fail with :
//java.lang.AssertionError: Expecting a javax.validation.ConstraintViolationException, but persist() succeed !
}
@Test
public void testEclipseLinkDefaultStrategy() { // Success
testPersistWithNullName("pu-eclipselink",new NameNotNullWithDefaultGeneratedStrategy());
}
@Test
public void testEclipseLinkTableStrategy() { // Success
testPersistWithNullName("pu-eclipselink",new NameNotNullWithTableGeneratedStrategy());
}
private void testPersistWithNullName(String persistenceUnitName, Object entity){
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName);
EntityManager entityManager = emf.createEntityManager();
try {
final EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
try {
try {
entityManager.persist(entity);
Assert.fail("Expecting a javax.validation.ConstraintViolationException, but persist() succeed !");
} catch (javax.validation.ConstraintViolationException cve) {
//That's expected
Assert.assertTrue("According JPA specs transaction must be flagged as rollback only",transaction.getRollbackOnly());
} catch (Exception e) {
Assert.assertTrue("According JPA specs transaction must be flagged as rollback only",transaction.getRollbackOnly());
e.printStackTrace();
Assert.fail("Expecting a javax.validation.ConstraintViolationException, but got " + e.getClass());
}
transaction.commit();
Assert.fail("persisted with null name !!!");
} catch (RollbackException e) {
//That's expected
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Unexpected exception :"+e.getMessage());
}
} finally {
entityManager.close();
}
}
}
实体
默认策略
package com.example.jpa.validator;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
@Entity
public class NameNotNullWithDefaultGeneratedStrategy {
@Id @GeneratedValue private Long id;
@NotNull public String name;
public NameNotNullWithDefaultGeneratedStrategy() {}
}
表策略:
package com.example.jpa.validator;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.TableGenerator;
import javax.validation.constraints.NotNull;
@Entity
public class NameNotNullWithTableGeneratedStrategy {
@GeneratedValue(strategy = GenerationType.TABLE,
generator = "NAME_MUST_NOT_BE_NULL_ID_GENERATOR")
@TableGenerator(name = "NAME_MUST_NOT_BE_NULL_ID_GENERATOR")
@Id @NotNull private Long id;
@NotNull public String name;
public NameNotNullWithTableGeneratedStrategy() {}
}
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="pu-hibernate" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.example.jpa.validator.NameNotNullWithTableGeneratedStrategy</class>
<class>com.example.jpa.validator.NameNotNullWithDefaultGeneratedStrategy</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test_mem_hibernate"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
</properties>
</persistence-unit>
<persistence-unit name="pu-eclipselink" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.example.jpa.validator.NameNotNullWithTableGeneratedStrategy</class>
<class>com.example.jpa.validator.NameNotNullWithDefaultGeneratedStrategy</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test_mem"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.H2Platform"/>
</properties>
</persistence-unit>
</persistence>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>com.example.jpa.validator</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<hibernate.version>4.2.0.CR1</hibernate.version>
<hibernate-validator.version>4.3.1.Final</hibernate-validator.version>
<junit.version>4.11</junit.version>
<h2.version>1.3.170</h2.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<url>http://download.eclipse.org/rt/eclipselink/maven.repo/</url>
<id>eclipselink</id>
<layout>default</layout>
<name>Repository for library EclipseLink (JPA 2.0)</name>
</repository>
</repositories>
</project>
我为此提交了一个错误报告:https :
//hibernate.onjira.com/browse/HHH-8028
这个问题是这个问题的后续:JPA约束违反vs回滚 我做了一些关于JPA和验证API(JSR-303)组合的测试。 我在JPA规范(第101-102页)中发现了以下内容: 默认情况下,默认Bean验证组(默认组)将在预持久性和预更新生命周期验证事件时进行验证 ... 如果validate方法返回的ConstraintViolation对象集不是空的,则持久性提供程序必须抛出javax。验证。Cons
但是,当我执行以下测试时,不会引发验证异常: 当保存一个已经持久化的对象时,我如何使验证器触发器?最好是通过Java配置。 我的验证依赖项:
作为一个相对的Java新手,我很困惑地发现了以下几点: 点. java: Edge.java: 主代码段:私有集blockedEdges 为什么这令人惊讶?因为在我将其编码为依赖平等之前,我检查了留档,它说: 如果该集合包含指定的元素,则返回true。更正式地说,当且仅当这个集合包含元素e,使得(o==null?e==null : o.equals(e)) 这句话非常清楚,它指出只需要相等。f.e
我尝试将JSF与Bean验证结合使用。基本上,一切正常,验证工作如预期,我得到了正确的消息,但我的Glassfish控制台上有一个例外: 如果我使用自定义约束和预定义约束,则会发生此异常。 这是我的示例代码。 示例实体: 自定义约束: 自定义验证器: 样品控制器: jsf页面示例: MyEntityFacade仅从实体管理器进行持久化调用。 如前所述,应用程序运行良好,正确的消息已显示,但我希望在
问题内容: 我正在使用Hibernate Validator 4.0.2,Spring 3.0和Hibernate 3.3.2(据我所知,是JPA2之前的版本)作为JPA 1提供程序。 我发现将Validator集成到MVC层很容易(这是可行的),但是看不到如何将验证器自动集成到JPA entityManager(JPA 1)中。 基本上,我有一些实体将保留下来,但它们不是来自Web层,因此尚未经
首先,我是Vaadin7的新手。当我发现BeanFieldGroup.class时,我正在尝试一些vaadin演示。正如我所看到的,这个类将一个字段绑定到一个bean属性。在bean中,使用验证约束注释对属性进行注释(JSR303)。在本例中,我的pom.xml包含hibernate验证器依赖项: 我创建了validationmessage.properties文件,并在其中放了一些消息(带有匈牙