I have a custom class with type parameters and it is not being invoked by constraint validator. Is there a way to invoke the validator for type parameters?
SpringController.java
@ResponseBody
@RequestMapping(method = RequestMethod.POST)
public EntityCollection<MyEntity> processData(
@Valid @RequestBody EntityCollection<MyEntity> entityRequestSet
, BindingResult bindingResult) throws Exception {
if(bindingResult.hasErrors())
{
// Problem here is that bindingResult has no errors, even though MyEntity
// has nulls in it. If I use just MyEntity as RequestBody instead of
// EntityCollection<MyEntity>, then the bindingResult has errors in it
// for fields with nulls
MethodParameter parameter = new MethodParameter(this.getClass()
.getMethod(new Object(){}.getClass().getEnclosingMethod().getName(),
EntityCollection.class, BindingResult.class), 0);
throw new MethodArgumentNotValidException(parameter, bindingResult);
}
return null;
}
EntityCollection.java
public class EntityCollection<MyEntity> extends GenericCollectionEntity<MyEntity> {
public EntityCollection() {
super();
}
public EntityCollection(
Collection<MyEntity> entities) {
super(entities);
}
}
GenericCollectionEntity.java
public abstract class GenericCollectionEntity<T> implements Serializable {
private static final long serialVersionUID = 1L;
public GenericCollectionEntity() {
super();
}
public GenericCollectionEntity(Collection<T> entities) {
super();
this.entities = entities;
}
protected Collection<T> entities;
public Collection<T> getEntities() {
return entities;
}
public void setEntities(Collection<T> entities) {
this.entities = entities;
}
}
MyEntity.java
@Entity
public class MyEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Valid
@EmbeddedId
private EntityKey key;
// getters & setters
}
EntityKey.java
@Embeddable
public class EntityKey implements Serializable {
private static final long serialVersionUID = 1L;
@NotNull
@Column(name = "ID")
private String id;
// ommitted other fields
//getters & setters
}
My bad, I couldn't apply my thinking on the basics. I just added
@Valid
to the field in abstract entity and it started validating the collection.GenericCollectionEntity.java