JPA 2.0:什么是 javax.validation.* 包? [英] JPA 2.0 : what is javax.validation.* package?
问题描述
什么是javax.validation
包在 Java EE?如何将其与 JPA 一起使用?
What is the javax.validation
package in Java EE? How to use this with JPA?
我想验证我的 JPA 实体 与 @NotNull
注释.我的 JPA 实现是 Hibernate.
I want to validate my JPA Entity with the @NotNull
annotation. My JPA implementation is Hibernate.
这是我的实体:
public class Employee implements Serializable , EmployeeDetail {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private int id;
private String name;
private long salary;
...
推荐答案
使用 Bean Validation Api,您可以非常轻松地确保对实体(或常见的 Bean)进行标准验证(非空、模式、电子邮件).
Using the Bean Validation Api you can quite easy ensure standard validation (not null, patterns, email) on your entities (or beans in common).
查看 Bean 验证 Api 的 Oracles 教程:http://docs.oracle.com/javaee/6/tutorial/doc/gircz.html
Check out Oracles tutorials for Bean Validation Api: http://docs.oracle.com/javaee/6/tutorial/doc/gircz.html
Hibernate 也应该没问题,因为它是参考实现:http://www.hibernate.org/subprojects/validator.html
Also Hibernate should be no problem with this, as it is the reference implementation: http://www.hibernate.org/subprojects/validator.html
有关此主题的良好介绍,请查看 Hibernates 教程:http://docs.jboss.org/hibernate/validator/4.0.1/reference/en/html_single/
For a good introduction check Hibernates tutorial on this topic: http://docs.jboss.org/hibernate/validator/4.0.1/reference/en/html_single/
其他一些教程(Google for JPA 2 + Bean Validation)
Some other tutorials (Google for JPA 2 + Bean Validation)
- http://www.jboss.org/jdf/examples/ticket-monster/教程/DataPersistence/
- http://musingsofaprogrammingaccident.blogspot.de/2010/01/jpa-2-and-bean-validation-in-action.html
JSR 规范...