JPA 2.0:什么是 javax.validation.* 包? [英] JPA 2.0 : what is javax.validation.* package?

查看:31
本文介绍了JPA 2.0:什么是 javax.validation.* 包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是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)

JSR 规范...

  • Bean 验证 1.1:

    这篇关于JPA 2.0:什么是 javax.validation.* 包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆