-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How to use the Hibernate Validator CDI portable extension
PostPosted: Thu May 05, 2016 9:04 am 
Newbie

Joined: Tue May 03, 2016 8:50 pm
Posts: 2
I would like to inject my original class to my customizing validator without JavaEE environment. I wrote a code for this considering the following points (#1 and #2) according to your reference guide, but I couldn’t get the instance injection (I got a NullPointerException in the isValid call).
How can I get the injection of UserServiceImpl.class, successfully?
Supposedly, ‘org.hibernate.validator.cdi.HibernateValidator’ should be used in somewhere else in the source code.
If my understanding is correct, please let me know how to use it in my codes.

#1. I added the Maven dependency to my POM as shown in Example1.3 in the reference guide.
#2. I prepared validator class and injection class as follows:
Code:
package xxxxx.constraints.impl;
import javax.inject.Inject;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import xxxxx.constraints.UnusedUserId;
import xxxxx.service.user.UserServiceImpl;

public class UnusedUserIdValidator implements ConstraintValidator<UnusedUserId, String> {
   @Inject
   UserServiceImpl userInformation;

   @Override
   public void initialize(UnusedUserId constraintAnnotation) {
   }

   @Override   
   public boolean isValid(String value, ConstraintValidatorContext context) {
      if (value == null) {
         return true;
      }
      return userInformation.isUnusedUserId(value);
   }
}

Code:
package xxxxx.service.user;

public interface UserService {
   boolean isUnusedUserId(String userId);

   // other methods
}

Code:
package xxxxx.service.user;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class UserServiceImpl implements UserService {

   @Override
   public boolean isUnusedUserId(String userId) {
// some logics
   }
}


Top
 Profile  
 
 Post subject: Re: How to use the Hibernate Validator CDI portable extension
PostPosted: Tue May 10, 2016 4:18 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
How are you obtaining your instance of javax.validation.Validator?

If you get it via dependency injection (@Inject), it should be properly set-up using the InjectingConstraintValidatorFactory. If you bootstrap your own Validator instance using the javax.validation.Validation API (e.g. in order to expose it through a custom producer method), you'd need to set InjectingConstraintValidatorFactory as the constraint validator factory. This factory will perform DI on the constraint validators it instantiates.

Hth,

--Gunnar

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 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.