如何使用eclipselink

如何使用eclipselink

本文介绍了如何使用eclipselink jpa验证模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,我需要使用 eclipselink jpa验证模型并且我添加注释@NotEmpty验证名称(如果为空),但是当我保存/持久化模型时,验证不起作用

I hava a model, and i need to validate model with eclipselink jpaand i add anotation @NotEmpty validate name if empty, but when i save/persist model, validate not work

  @Entity
    public class Role {

        @Id
        private String id;
        @NotEmpty
        private String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }

我的jpa配置xml

my jpa configuration xml like this

<?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="Eclipselink_JPA" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>app.test.Model.Role</class>

        <properties>

            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value=""/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>


        </properties>
    </persistence-unit>
</persistence>

推荐答案

JPA API(由EclipseLink实现)与 Bean验证API .要使用Bean验证API,您需要Bean验证API jar(javax.validation),以及CLASSPATH中该API的实现(Apache BVALHibernate Validator等).JPA唯一提供的是根据此链接,但是默认值为自动",因此实际上不需要任何操作

The JPA API (which EclipseLink implements) is nothing to do with the Bean Validation API.To utilise the Bean Validation API you need the Bean Validation API jar (javax.validation), together with an implementation of that API (Apache BVAL, Hibernate Validator, etc) in your CLASSPATH.The only thing JPA provides is to auto-enable validation as per this link, but the default is "auto" so nothing required really for that

这篇关于如何使用eclipselink jpa验证模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:16