[英]How to activate automatic JPA entity validation in Java SE?
I activated entity validation in persistence.xml
with 我使用以下命令在persistence.xml
激活了实体验证
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="de.richtercloud_hibernate-automatic-validation_jar_1.0-SNAPSHOTPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<validation-mode>AUTO</validation-mode>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:myDB;create=true"/>
<property name="javax.persistence.jdbc.user" value="app"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.password" value="app"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
and declared the Maven dependencies 并声明了Maven依赖项
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.11.Final</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.14.2.0</version>
</dependency>
Assuming I have a very simple 假设我有一个非常简单的
@Entity
public class Entity0 implements Serializable {
@Id
private Long id;
@Basic
@NotNull
private String property0;
public Entity0() {
}
public Entity0(Long id,
String property0) {
this.id = id;
this.property0 = property0;
}
[getter and setter for id and property0]
}
and persist it with 并坚持下去
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("de.richtercloud_hibernate-automatic-validation_jar_1.0-SNAPSHOTPU");
EntityManager entityManager = entityManagerFactory.createEntityManager();
Entity0 entity0 = new Entity0(1l, "abc");
entityManager.getTransaction().begin();
entityManager.persist(entity0);
entityManager.flush();
entityManager.getTransaction().commit();
entityManager.close();
entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entity0 = entityManager.merge(entity0);
entity0.setProperty0(null);
entityManager.merge(entity0);
entityManager.flush();
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
Why does entityManager.merge(entity0); entityManager.flush();
为什么entityManager.merge(entity0); entityManager.flush();
entityManager.merge(entity0); entityManager.flush();
not cause a validation constraint violation of the automatically invoked validator after entity0.setProperty0(null);
在entity0.setProperty0(null);
之后不会导致验证约束违反自动调用的验证器entity0.setProperty0(null);
? ? Afaik, the presence of a validation implementation on the classpath enables the JPA entity validation. Afaik,在类路径上存在验证实现可启用JPA实体验证。
Hibernate as JPA and Hibernate Validator are example implementations. 作为JPA的Hibernate和Hibernate Validator是示例实现。 I assume there's a solution for all JPA providers and JSR 303 implementations. 我假设有一个针对所有JPA提供程序和JSR 303实现的解决方案。
I'm aware that I can validate the entities manually which is out of the scope of the question. 我知道我可以手动验证实体,这超出了问题的范围。
An SSCCE is provided at https://gitlab.com/krichter/hibernate-automatic-validation . 在https://gitlab.com/krichter/hibernate-automatic-validation上提供了SSCCE。 It does not contain more information than the MCVE above and merely allows to reproduce the issue locally as easily as possible. 它不包含比上面的MCVE更多的信息,仅允许在本地尽可能轻松地重现问题。
I needed to add 我需要添加
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.6</version>
</dependency>
to the list of dependencies. 到依赖项列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.