I\'m trying to add constraints checking, as described here How to specify the cardinality of a @OneToMany in EclipseLink/JPA
The dependencies as of 2019:
org.hibernate.validator
hibernate-validator
6.0.16.Final
This transitively pulls in the dependency to the Bean Validation API, so you don't need to do this anymore:
javax.validation
validation-api
1.1.0.Final
For additional features, Expression Language and CDI support, you might need to add:
org.glassfish
javax.el
3.0.1-b09
org.hibernate.validator
hibernate-validator-cdi
6.0.16.Final
Source: Hibernate Validator documentation
These are all in Maven Central Repo, so you don't need to add the JBoss repo.
And BTW here's my example convenience method:
public static void validate( T object ) throws MigrationException
{
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set> valRes = validator.validate( object );
if( ! valRes.isEmpty() )
{
StringBuilder sb = new StringBuilder("Validation failed for: ");
if( object instanceof Origin.Wise )
sb.append( ((Origin.Wise)object).getOrigin() );
else
sb.append(object);
for( ConstraintViolation fail : valRes)
{
sb.append("\n ").append( fail.getMessage() );
}
throw new IllegalArgumentException( sb.toString() );
}
}
The Origin.Wise
is something like JAXB's @XmlLocation Locator
.
In 2013 (the original post) the versions were:
org.hibernate
hibernate-validator
5.4.0.Final
org.glassfish
javax.el
3.0.1-b08
org.hibernate
hibernate-validator-cdi
5.4.0.Final