Hi,
I have hierarchy of classes (JPA Entities) on which my business service does two levels of validation. At the beginning preliminary validation is performed to check if the fields required for computation are provided. Later some business computation occurs which fills some other fields and at the end (on Hibernate save/update) default/full validation is performed (all provided by user and computed fields are required).
Usually I specified that some fields are required only on validation with given validation group, which required only to add group=MyGroup to @NotNull or other annotation. Here I would like to validate all fields by default and
skip selected fields on validation with given group. I was able to achieve that with defining @NotNull(groups = {Default.class} for fields that should be skipped on preliminary validation and @NotNull(groups = {Defaut.class, PreComputationGroup.class} for the other fields validated in any case.
Unfortunately it's a class hierarchy with many fields and it doesn't look good, is not intuitive and error prone on modifications by other people. I tried with redefining groups (@GroupSequence), groups hierarchy and some other cases, but without success. Maybe you could suggest some better, more generic solution to my case?
Minimal code sample for better visualization:
Code:
interface PreComputationGroup {}
class MyEntity {
@NotNull(groups = {Default.class, PreComputationGroup.class}
private String fieldProvidedByUserRequiredOnComputationAndOnPersist;
@NotNull(groups = Default.class)
private String calculatedFieldRequiredOnlyOnPersist; //should be skipped at PreComputation validation
...
}
class MyService {
void doBusiness(MyEntity myEntity) {
validator.validate(myEntity, PreComputationGroup.class);
doComputationAndFillAdditionalFields(myEntity);
session.save(myEntity); //full/default validation is performed
}
}