How to I get Spring-Data-MongoDB to validate my objects?

First make sure that you have JSR-303 validator on classpath, for example: <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.2.0.Final</version> </dependency> If you use Java config, the way to go is to create 2 beans: @Bean public ValidatingMongoEventListener validatingMongoEventListener() { return new ValidatingMongoEventListener(validator()); } @Bean public LocalValidatorFactoryBean validator() { return new LocalValidatorFactoryBean(); } VoilĂ ! Validation is working now.

Autowired Repository is Null in Custom Constraint Validator

Which ValidatorFactory are you using. Or to put it another way, hot to you bootstrap Bean Validation? Per default Bean Validation (and Hibernate Validator as reference implentation) does not inject dependencies into ConstraintValidator instances. At least in Bean Validation 1.0. To enable dependency injection you have to configure a custom ConstraintValidatorFactory. Spring offers SpringConstraintValidatorFactory which … Read more

hibernate unique key validation

One of the possible solutions is to create custom @UniqueKey constraint (and corresponding validator); and to look-up the existing records in database, provide an instance of EntityManager (or Hibernate Session)to UniqueKeyValidator. EntityManagerAwareValidator public interface EntityManagerAwareValidator { void setEntityManager(EntityManager entityManager); } ConstraintValidatorFactoryImpl public class ConstraintValidatorFactoryImpl implements ConstraintValidatorFactory { private EntityManagerFactory entityManagerFactory; public ConstraintValidatorFactoryImpl(EntityManagerFactory entityManagerFactory) { this.entityManagerFactory … Read more

JSR303 custom validators being called twice

Maybe the second validation is done by hibernate when you are sending your bean to the datastore. To turn it off add this to your persistence.xml: <property name=”javax.persistence.validation.mode” value=”none”/> https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/configuration.html says: By default, Bean Validation (and Hibernate Validator) is activated. When an entity is created, updated (and optionally deleted), it is validated before being sent … Read more

Inject Service in ConstraintValidator [Bean Validator – JSR 303] Spring

You should set up a LocalValidatorFactoryBean in your Spring application context so you can retrieve Validator (and ValidatorFactory) instances via dependency injection. By default, a validator obtained like that makes use of SpringConstraintValidatorFactory which provides dependency injection services to constraint validator implementations, as pointed out by Pangea.

Spring MVC – @Valid on list of beans in REST service

@Valid is a JSR-303 annotation and JSR-303 applies to validation on JavaBeans. A java.util.List is not a JavaBean (according to the official description of a JavaBean), hence it cannot be validated directly using a JSR-303 compliant validator. This is supported by two observations. Section 3.1.3 of the JSR-303 Specification says that: In addition to supporting … Read more

Annotations from javax.validation.constraints not working

For JSR-303 bean validation to work in Spring, you need several things: MVC namespace configuration for annotations: <mvc:annotation-driven /> The JSR-303 spec JAR: validation-api-1.0.0.GA.jar (looks like you already have that) An implementation of the spec, such as Hibernate Validation, which appears to be the most commonly used example: hibernate-validator-4.1.0.Final.jar In the bean to be validated, … Read more