Today when a property marked @Id @GeneratedValue is also marked @NotNull, it will fail if the identity generation is post-insert in the database. This can be the case if the underlying table is using column id generation for example.
The id property is thus null when Bean Validation is executed during the pre-persist phase and a constraint violation is raised.
One option is to stay put and consider the behavior valid. The user can work around that by:
@NotNullannotation as belonging to the group
override the groups in
Here is the snipped of the
<property name="javax.persistence.validation.group.pre-persist" value="javax.validation.groups.Default"/> <property name="javax.persistence.validation.group.pre-update" value="javax.validation.groups.Default,com.acme.groups.Created"/>
We could use option 1 but have JPA provide the created group:
JPA could disable validation on id properties if in pre-persist and if the generated value is not created yet.
Note that we disable all validations - not simply
@NotNull - as it does not make much sense to execute other
validations when a property is null.
TraversableResolver can be used to solve the problem as
isReachable can stop the processing of a property.
Option 3 seems the most reasonable but it could be complicated at the JPA level. Before going to the JPA EG, let’s discuss the situation here.