0

I'm running an application in Tomcat using Weld (org.jboss.weld.servlet:weld-servlet:2.0.2.Final with maven) to produces ValidatorFactory and Validator components like this:

@Produces 
@Default 
@javax.enterprise.context.ApplicationScoped
public ValidatorFactory getInstance() {
    return Validation.byDefaultProvider().configure().buildValidatorFactory();
}

And

@Produces @javax.enterprise.context.ApplicationScoped
public Validator getInstanceValidator() {
    return factory.getValidator();
}

When I inject Validator and calls method Validator.forExecutables(), I'm getting this exception:

org.jboss.weld.proxies.Validator$1284425531$Proxy$_$$_WeldClientProxy 
cannot be cast to javax.validation.executable

But if I inject ValidatorFactory and calls ValidatorFactory.getValidator().forExecutables(), all works fine.

There are wrong in my code? Or there are another procedure to avoid this error?

EDIT

Analyzing Hibernate validator source code, I see that ValidatorImpl implements both Validator and ExecutableValidator interface. When weld creates the proxy, it implements only methods found in Validator interface, and not ExecutableValidator methods.

There are way to resolve this?

Otávio Garcia
  • 1,324
  • 1
  • 14
  • 27

1 Answers1

1

Which version of Hibernate Validator is this?

Actually Hibernate Validator's CDI ValidatorBean returns ExecutableValidator as type as well, so the proxy should be created for this. I vaguely remember that we had issues with that in a Beta release, so it might be you're still using that one.

Gunnar
  • 15,675
  • 1
  • 44
  • 63
  • Gunnar, should I use hibernate-validator-cdi instead of hibernate-validator? – Otávio Garcia Sep 17 '13 at 01:19
  • Yes, definitely use _hibernate-validator-cdi_. This is a CDI portable extension which we provide and which gives you integration between Bean Validation and CDI. That way you also don't need your own `ValidatorFactory` producer method, as the extension provides beans for `Validator` and `ValidatorFactory`. – Gunnar Sep 17 '13 at 06:47
  • So is it correct to say that I should not use hibernate-validator jar and instead use hibernate-validator-cdi jar? I am trying to solve a similar problem: http://stackoverflow.com/questions/40555721/unable-to-get-cdi-working-in-weblogic-12c-hibernate-5-2-4-hibernate-validator. Please advise. – donlys Nov 12 '16 at 15:03