0

JPA implementation by default check validation constrains at the following lifecycle points:

javax.persistence.validation.group.pre-persist
javax.persistence.validation.group.pre-update
javax.persistence.validation.group.pre-remove

In a spring boot application what's the way to customize this behaviour ?
I don't want to use a persistence.xml file

Also, is there a way to enable the check also when an Entity is loaded ?
Like if I'm using @NotNull I also want to declare that any instance of that entity,
with that property null is just invalid, so an exception should be thrown ?

GionJh
  • 2,742
  • 2
  • 29
  • 68

1 Answers1

0

You should be able to modify these persistence properties in a spring boot application by prefixing them with spring.jpa.properties followed by their values in the application.properties file.

For example, javax.persistence.validation.group.pre-update would be spring.jpa.properties.javax.persistence.validation.group.pre-update followed by the groups you wish to validate using these lifecycle events as illustrated below:

spring.jpa.properties.javax.persistence.validation.group.pre-update=javax.validation.groups.Default

With regards to the data integrity of your application, ideally you would want to ensure you only have clean, accurate date present in the underlying database and that there are DB constraints in place to prevent any incorrect data from being present. For instance, you would want to have a NOT NULL constraint on that specific column in the DB table. If you are generating your DB schema from the Java code you might consider using the @NotNull along with the @Column(nullable = false) annotation which would generate this constraint for you

Jawad
  • 361
  • 3
  • 8