First off, I'm not sure if Hibernate validates single properties in isolation? Doesn't it revalidate the entire bean every time? This means if you changed the superset and the subset property was no longer a subset, then the subset property would not pass validation since Hibernate would recheck it before doing anything with the object.
However, if my assumption is false and Hibernate does (or can) validate properties in isolation, then why not introduce the idea of dependencies into Hibernate's Validator interface? Hibernate's Validator interface currently defines two methods: isValid and initialize. Why not modify 'initialize' to look something like this:
Code:
public ValidationDependency[] initialize(A parameters);
ValidationDependency could be a simple class (for now):
Code:
public class ValidationDependency implements Serializable {
private final String propertyDependency;
public ValidationDependency(final String propertyDependency) {
this.propertyDependency = propertyDependency;
}
public String getPropertyDependency() {
return this.propertyDependency;
}
}
In my initialize method for the @SubsetOf validator, I could do this:
Code:
public ValidationDependency[] initialize(SubsetOf parameters) {
...
return new ValidationDependency[] { new ValidationDependency(parameters.superset()) };
}
Hibernate could then ensure that whenever a property with dependencies is validated, all dependent properties get validated as well.