EclipseLink:如何在没有 JEE/CDI 的情况下启用 Bean 验证? (又名在 SE 中启用)

EclipseLink: How to enable Bean Validation without JEE/CDI? (aka enabling in SE)

我们有一个测试扩展,它为我们的测试创建了一个 EntityManager:

Map<String, String> properties = new HashMap<String, String>(); properties.put(PersistenceUnitProperties.JDBC_DRIVER, DRIVER_NAME); properties.put(PersistenceUnitProperties.JDBC_URL, createUrl(testInstance)); properties.put(PersistenceUnitProperties.JDBC_USER, DB_USER); properties.put(PersistenceUnitProperties.COORDINATION_PROTOCOL, null); emf = Persistence.createEntityManagerFactory(PU_NAME, properties); ... EntityManager em = emf.createEntityManager();

我们注意到,虽然我们的其他验证器运行良好,但没有 BeanValidator 在 pre-persist 或 pre-update 上触发。这是有道理的,在JEE容器中,Bean Validation库是容器自己提供的。

如果我们将它包含在测试类路径中,是否可以让 EclipseLink 调用类似 OpenWebBeans 的东西?

这就像将它添加到我们的 pom 中一样简单:

    <dependency>
        <groupId>org.apache.bval</groupId>
        <artifactId>bval-jsr</artifactId>
        <version>1.1.2</version>
        <scope>test</scope>
    </dependency>