Derit Agustin
Derit Agustin

Reputation: 778

How to validate model with eclipselink jpa

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>

Upvotes: 1

Views: 2921

Answers (1)

Neil Stockton
Neil Stockton

Reputation: 11531

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

Upvotes: 4

Related Questions