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

Hibernate validator: @Email accepts ask@stackoverflow as valid?

You can also use constraint composition as a work-around. In the example below, I rely on the @Email validator to do the main validation, and add a @Pattern validator to make sure the address is in the form of [email protected] (I don’t recommend using just the @Pattern below for regular Email validation) @Email(message=”Please provide a … Read more

AbstractMethodError on deploying Spring 4.0 in Tomcat 6

The error has nothing to do with the EL. It has all to do with the javax.validation api and hibernate. java.lang.AbstractMethodError: org.hibernate.validator.internal.engine.ConfigurationImpl.getDefaultParameterNameProvider()Ljavax/validation/ParameterNameProvider Hibernate validator 4.3.x is an implementation of javax.validation 1.0 (JSR-303). However you are including the 1.1 API version. Either downgrade the included javax.validation version or upgrade your hibernate validator to 5.0.x.

Autowired gives Null value in Custom Constraint validator

I hope the solution will help someone: @Bean public Validator validator () { ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class ) .configure().constraintValidatorFactory(new SpringConstraintValidatorFactory(autowireCapableBeanFactory)) .buildValidatorFactory(); Validator validator = validatorFactory.getValidator(); return validator; } Initializing the validator with SpringConstraintValidatorFactory so that injection works and providing the validator implementation to be Hibernate.class works in the following manner: Your objects will be … Read more

javax.validation.ValidationException: HV000183: Unable to load ‘javax.el.ExpressionFactory’

It is working after adding to pom.xml following dependencies: <dependency> <groupId>javax.el</groupId> <artifactId>javax.el-api</artifactId> <version>2.2.4</version> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>javax.el</artifactId> <version>2.2.4</version> </dependency> Getting started with Hibernate Validator: Hibernate Validator also requires an implementation of the Unified Expression Language (JSR 341) for evaluating dynamic expressions in constraint violation messages. When your application runs in a Java EE container such … Read more

Hibernate Validation of Collections of Primitives

Neither JSR-303 nor Hibernate Validator has any ready-made constraint that can validate each elements of Collection. One possible solution to address this issue is to create a custom @ValidCollection constraint and corresponding validator implementation ValidCollectionValidator. To validate each element of collection we need an instance of Validator inside ValidCollectionValidator; and to get such instance we … Read more

Cross field validation with Hibernate Validator (JSR 303)

Each field constraint should be handled by a distinct validator annotation, or in other words it’s not suggested practice to have one field’s validation annotation checking against other fields; cross-field validation should be done at the class level. Additionally, the JSR-303 Section 2.2 preferred way to express multiple validations of the same type is via … Read more