How to validate model with eclipselink jpa

孤人 提交于 2019-12-11 07:03:03

问题


I hava a model, and i need to validate model with eclipselink jpa and 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;
        }

    }

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>

回答1:


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



来源:https://stackoverflow.com/questions/45370398/how-to-validate-model-with-eclipselink-jpa

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