I have a dummy JPA Entity to demonstrate my need:
Code:
@Entity
public class User {
private boolean referenced;
private String referencedBy;
private Date referenceTime;
...
}
As i understand it, i can annotate those fields with bean validation annotations. Also, with BV integration with JPA, i can make groups that belong to one of the three lifecycle validation groups (pre-persist, pre-update and pre-remove).
In my above example, my need is :
1. If the referenced == true
2. Then referencedBy and referenceTime must not be null on pre-persist and pre-update
Obviously this example below wont work, as the validation would be done for referencedBy and referenceTime on pre-persist and pre-update :
Code:
@Entity
public class User {
private boolean referenced;
@NotNull
private String referencedBy;
@NotNull
private Date referenceTime;
...
}
Any idea on how to solve this ? Or this kind of validation should belong not in the bean validation, but as a code somewhere like in @PrePersist method ?
Thank you !