Javax validation on nested objects – not working
Just try adding @valid to collection. it would be working as per reference hibernate @Getter @Setter @Valid @NotNull(groups = { Existing.class }) private List<LocationDto> locations;
Just try adding @valid to collection. it would be working as per reference hibernate @Getter @Setter @Valid @NotNull(groups = { Existing.class }) private List<LocationDto> locations;
Actually, according to the specification, adding @Valid is exactly for this usecase. From the JSR 303 specification: In addition to supporting instance validation, validation of graphs of object is also supported. The result of a graph validation is returned as a unified set of constraint violations. Consider the situation where bean X contains a field … Read more
Annotating getters doesn’t mean that validation is performed when a getter is invoked. It is just used to identify the property to which a constraint shall apply. The big advantage of putting constraints on (usually public) getters instead on (typically private) fields is that the constraints are part of the type’s public API that way. … Read more
Unless you acquire a lock on a whole table, it is basically not possible to check for unicity using a SQL query (any concurrent transaction could modify data after a manual check but before the commit of the ongoing transaction). In other words, it isn’t possible to implement a valid unique verification at the Java … Read more
@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
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
NotEmpty is just a combination of @NotNull and @Size(min=1).
The factory-method should only contain the method name, not including the class name. If you want to use a static factory, give the class of the factory(!) to the bean declaration, if you want to use an instance factory, give the factory-bean to the bean declaration, but don’t give both: The class of the created … Read more
I created simple class: public class Mock { @Size(min=1, max=3) private List<String> strings; public List<String> getStrings() { return strings; } public void set(List<String> strings) { this.strings = strings; } } And test: Mock mock = new Mock(); mock.setStrings(Collections.emptyList()); final Set<ConstraintViolation<Mock>> violations1 = Validation.buildDefaultValidatorFactory().getValidator().validate(mock); assertFalse(violations1.isEmpty()); mock.setStrings(Arrays.asList(“A”, “B”, “C”, “D”)); final Set<ConstraintViolation<Mock>> violations2 = Validation.buildDefaultValidatorFactory().getValidator().validate(mock); assertFalse(violations2.isEmpty()); It … Read more
There are 2 compliant implementations now: Apache Bean Validation (formerly agimatec) Hibernate Validator You can access a benchmark and a quick description of both here: http://carinae.net/2010/06/benchmarking-hibernate-validator-and-apache-beanvalidation-the-two-jsr-303-implementations/