Hibernate 作为 TomEE 7 中的 JPA 提供者和 Bean 验证器

Hibernate as the JPA provider and Bean Validator in TomEE 7

JPA

配置 Hibernate 5.2 作为 Apache Tomcat (TomEE)/8.5.11 (7.0.3) 来自 this Article (TomEE 7 and Hibernate 5.2 persistence.xml)

Bean 验证

要将 Hibernate Validator 实现为 Bean Validator,将 hibernate-validator-5.4.0.Final.jarhibernate-validator-annotation-processor-5.4.0.Final.jarhibernate-validator-cdi-5.4.0.Final.jar 放在 /lib 文件夹中并删除 bval-core-1.1.2.jarbval-jsr-1.1.2.jar

但是在部署 webapp 时出现以下错误:

DEBUG: No META-INF/validation.xml found. Using annotation based configuration only.
19-May-2017 09:16:14.276 SEVERE [localhost-startStop-1] org.apache.openejb.cdi.OpenEJBLifecycle.startApplication CDI Beans module deployment failed
org.apache.webbeans.exception.WebBeansDeploymentException: javax.enterprise.inject.AmbiguousResolutionException: There is more than one Bean with type javax.validation.ValidatorQualifiers: [@javax.enterprise.inject.Default()]
for injection into Field Injection Point, field name :  validator, Bean Owner : [ValidationInterceptor, WebBeansType:INTERCEPTOR, Name:null, API Types:[java.io.Serializable,java.lang.Object,org.hibernate.validator.internal.cdi.interceptor.ValidationInterceptor], Qualifiers:[javax.enterprise.inject.Default,javax.enterprise.inject.Any]]
found beans: 
Validator, WebBeansType:VALIDATION, Name:null, API Types:[java.lang.Object,javax.validation.Validator], Qualifiers:[javax.enterprise.inject.Default,javax.enterprise.inject.Any] from jar:file:/webserver/tomee/lib/javaee-api-7.0-1.jar!/javax/validation/Validator.class
ValidatorImpl, WebBeansType:THIRDPARTY, Name:null, API Types:[org.hibernate.validator.internal.engine.ValidatorImpl,javax.validation.executable.ExecutableValidator,java.lang.Object,javax.validation.Validator], Qualifiers:[javax.enterprise.inject.Default,org.hibernate.validator.cdi.HibernateValidator,javax.enterprise.inject.Any] from jar:file:/webserver/tomee/lib/hibernate-validator-5.4.0.Final.jar!/org/hibernate/validator/internal/engine/ValidatorImpl.class
at org.apache.webbeans.config.BeansDeployer.deploy(BeansDeployer.java:331)
at org.apache.openejb.cdi.OpenEJBLifecycle.startApplication(OpenEJBLifecycle.java:196)
at org.apache.openejb.cdi.ThreadSingletonServiceImpl.initialize(ThreadSingletonServiceImpl.java:189)
at org.apache.openejb.cdi.CdiBuilder.build(CdiBuilder.java:41)
at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:914)
at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:718)
at org.apache.tomee.catalina.TomcatWebAppBuilder.startInternal(TomcatWebAppBuilder.java:1298)
at org.apache.tomee.catalina.TomcatWebAppBuilder.configureStart(TomcatWebAppBuilder.java:1124)
at org.apache.tomee.catalina.GlobalListenerSupport.lifecycleEvent(GlobalListenerSupport.java:133)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5087)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:596)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1805)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.enterprise.inject.AmbiguousResolutionException: There is more than one Bean with type javax.validation.ValidatorQualifiers: [@javax.enterprise.inject.Default()]
for injection into Field Injection Point, field name :  validator, Bean Owner : [ValidationInterceptor, WebBeansType:INTERCEPTOR, Name:null, API Types:[java.io.Serializable,java.lang.Object,org.hibernate.validator.internal.cdi.interceptor.ValidationInterceptor], Qualifiers:[javax.enterprise.inject.Default,javax.enterprise.inject.Any]]

如何解决这个问题????

认为它来自您包含 hibernate-validator 的事实,您可以在 application.properties (WEB-INF) 或 conf/system.properties(全局)中解决它的设置:

org.apache.bval.cdi.BValExtension.active=false

如 Hibernate Validator 文档 (here) 中所述,如果应用程序在不提供开箱即用的 CDI 集成的环境中运行,您可以通过添加 hibernate-validator-cdi

Apache Tomcat (TomEE)/8.5.11 (7.0.3) 使用开箱即用的 Apache OpenWebBeans 实现 CDI。

<TomEE>/lib 文件夹中删除 hibernate-validator-cdi-5.4.0.Final.jar 后问题已解决。