java JPA 实体验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3323111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA Entity Validation
提问by s.susini
Ok, I'm trying to setup an application in a Java EE Container. I use JPA for persistence and I also use the javax.validation.constraints.*constraints. By default the container validate the entities during the @PrePersistand @PreUpdatelifecycle events and it's good for me, but how do I handle the ConstraintViolationException?
好的,我正在尝试在 Java EE 容器中设置应用程序。我将 JPA 用于持久性,并且我也使用javax.validation.constraints.*约束。默认情况下,容器在@PrePersist和@PreUpdate生命周期事件期间验证实体,这对我有好处,但是我该如何处理ConstraintViolationException?
I can't find any docs on it, any suggestion is welcomed.
我找不到任何文档,欢迎提出任何建议。
回答by Pascal Thivent
Well, you could catch it :) Here is an example (from a unit test):
好吧,你可以抓住它:) 这是一个例子(来自单元测试):
public class CustomerTest {
private static EntityManagerFactory emf;
private EntityManager em;
@BeforeClass
public static void createEntityManagerFactory() {
emf = Persistence.createEntityManagerFactory("MyPu");
}
@AfterClass
public static void closeEntityManagerFactory() {
emf.close();
}
@Before
public void beginTransaction() {
em = emf.createEntityManager();
em.getTransaction().begin();
}
@After
public void rollbackTransaction() {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if (em.isOpen()) {
em.close();
}
}
@Test
public void nameTooShort() {
try {
Customer customer = new Customer("Bo");
em.persist(customer);
em.flush();
fail("Expected ConstraintViolationException wasn't thrown.");
} catch (ConstraintViolationException e) {
assertEquals(1, e.getConstraintViolations().size());
ConstraintViolation<?> violation = e.getConstraintViolations().iterator().next();
assertEquals("name", violation.getPropertyPath().toString());
assertEquals(Size.class, violation.getConstraintDescriptor().getAnnotation().annotationType());
}
}
}
Where my Customer looks like:
我的客户看起来像:
@Entity
public class Customer {
@Id @GeneratedValue
@NotNull
private Long id;
@NotNull
@Size(min = 3, max = 80)
private String name;
private boolean archived;
...
}
But this was just an example to show a tiny part of the API.
但这只是展示 API 一小部分的示例。
In my opinion, you should actually handle the validation at the view level. Many presentation frameworks support Bean Validation: JSF 2.0, Wicket, Spring MVC...
在我看来,您实际上应该在视图级别处理验证。许多表示框架都支持 Bean Validation:JSF 2.0、Wicket、Spring MVC...
See also
也可以看看
回答by pihentagy
You can catch ConstraintViolationException, as Pascaldescribed it, but please be aware, that this Exception could be thrown anytimeafter your create/update statement, and before the end of the transaction (see FlushModeType). Typical throwing points are when you read something after your create/update statement, or at an explicit flush()statement (which you should use with caution).
你能赶上ConstraintViolationException,如帕斯卡尔描述它,但请注意,这个异常可能会被抛出任何时候你创建/更新语句之后,交易结束前(见FlushModeType)。典型的问题是当您在创建/更新语句之后或在显式flush()语句(您应该谨慎使用)处阅读某些内容时。

