Hibernate Books

All times are UTC - 5 hours [ DST ]

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Using InjectingConstraintValidatorFactory
PostPosted: Wed Apr 19, 2017 11:21 am 

Joined: Sat Mar 17, 2012 1:33 pm
Posts: 5

In Resteasy, we call


to get a CDI aware ValidatorFactory. Recently, we have had a request to support Validation and CDI in Java SE, and I can get an instance of InjectingConstraintValidatorFactory by calling
                  HibernateValidatorConfiguration config = Validation.byProvider(HibernateValidator.class).configure();
                  ConstraintValidatorFactory cvf = null;
                     Class<?> cvfClass = config.getClass().getClassLoader().loadClass("org.hibernate.validator.internal.cdi.InjectingConstraintValidatorFactory");
                     if (cvfClass != null)
                        BeanManager beanManager = CDI.current().getBeanManager();
                        Constructor<?> constructor = cvfClass.getConstructor(BeanManager.class);
                        cvf = (ConstraintValidatorFactory) constructor.newInstance(beanManager);
                        validatorFactory = tmpValidatorFactory = config.constraintValidatorFactory(cvf).buildValidatorFactory();

Is there a more elegant, safer way of doing that, maybe by way of a different call to Validation.byProvider()?


 Post subject: Re: Using InjectingConstraintValidatorFactory
PostPosted: Fri Apr 21, 2017 5:43 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 364
Hi Ron,

If the HV portable extension for CDI is present, you should be able to just obtain a ValidatorFactory correctly set up from the BeanManager or even the CDI class itself:


Could you give this a try?



Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.