2017-07-28 62 views
0

我哈瓦的模型,我需要驗證模型eclipselink JPA 和我添加如果空anotation @NotEmpty驗證的名字,但是當我保存/持續模型,驗證不行如何驗證與EclipseLink的JPA模型

@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; 
     } 

    } 

我喜歡這個

<?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

JPA的API JPA配置XML(這EclipseLink工具)是無關的Bean Validation API。 要使用Bean驗證API,您需要在您的CLASSPATH中使用Bean驗證API jar(javax.validation)以及該API的實現(Apache BVALHibernate Validator等)。 JPA提供的唯一功能是根據this link自動啓用驗證,但默認爲「自動」,因此沒有任何要求,因此

+0

使用Hibernate驗證器和javax驗證解決 –