JPA 2.0 : what is javax.validation.* package?

假如想象 提交于 2019-12-18 03:56:32

问题


What is the javax.validation package in Java EE? How to use this with JPA?

I want to validate my JPA Entity with the @NotNull annotation. My JPA implementation is Hibernate.

Here's my Entity:

public class Employee implements Serializable , EmployeeDetail {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private int id;

    private String name;

    private long salary;
    ...

回答1:


Using the Bean Validation Api you can quite easy ensure standard validation (not null, patterns, email) on your entities (or beans in common).

Check out Oracles tutorials for Bean Validation Api: http://docs.oracle.com/javaee/6/tutorial/doc/gircz.html

Also Hibernate should be no problem with this, as it is the reference implementation: http://www.hibernate.org/subprojects/validator.html

For a good introduction check Hibernates tutorial on this topic: http://docs.jboss.org/hibernate/validator/4.0.1/reference/en/html_single/

Some other tutorials (Google for JPA 2 + Bean Validation)

  • http://www.jboss.org/jdf/examples/ticket-monster/tutorial/DataPersistence/
  • http://musingsofaprogrammingaddict.blogspot.de/2010/01/jpa-2-and-bean-validation-in-action.html

JSR specifications…

  • Bean Validation 1.1:
    • spec
    • JSR 349
  • Bean Validation 1.0
    • JSR 303


来源:https://stackoverflow.com/questions/14729234/jpa-2-0-what-is-javax-validation-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!