Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate validator - different groups on create, update, delete

Using bean validation, particular hibernate validator implementation is it possible to define certain groups to automatically be used on certain crud operations like create or update?

or are there some build in hibernate groups that are internally checked for those operations?

like image 957
djmj Avatar asked Jun 05 '13 02:06

djmj


People also ask

Is Hibernate Validator thread safe?

Validating constraints In the setUp() method, a Validator instance is retrieved from the ValidatorFactory . Validator instances are thread-safe and may be reused multiple times. The validate() method returns a set of ConstraintViolation instances, which you can iterate in order to see which validation errors occurred.

How does Hibernate Validator work?

Hibernate Validator allows to express and validate application constraints. The default metadata source are annotations, with the ability to override and extend through the use of XML. It is not tied to a specific application tier or programming model and is available for both server and client application programming.

Which exception is thrown when a PATH variable validation fails a ConstraintViolationException?

In contrast to request body validation a failed validation will trigger a ConstraintViolationException instead of a MethodArgumentNotValidException . Spring does not register a default exception handler for this exception, so it will by default cause a response with HTTP status 500 (Internal Server Error).

What is @valid Annotation in spring boot?

The @Valid annotation ensures the validation of the whole object. Importantly, it performs the validation of the whole object graph. However, this creates issues for scenarios needing only partial validation. On the other hand, we can use @Validated for group validation, including the above partial validation.


1 Answers

You're probably looking for "Hibernate event-based validation" under "ORM Integration". You can set properties to specify which groups to validate at different times by setting properties on the SessionFactory like so:

<property name="javax.persistence.validation.group.pre-persist">javax.validation.Default</property>
<property name="javax.persistence.validation.group.pre-update">javax.validation.Default</property>
<property name="javax.persistence.validation.group.pre-remove"></property>

The above is the default configuration if you don't specify anything. Specifically, the javax.validation.Default group is validated on creates and updates. Nothing is validated on deletes.

like image 68
Ryan Stewart Avatar answered Oct 31 '22 16:10

Ryan Stewart