How to avoid automatic entity update on Hibernate ValidationException

I having a spring method: where I am validating the entity after constructing of object, which was previously fetched from DB.

    public Object approve(Command command) throws ValidationException {
        Object obj = merger.mergeWithExistingobject(command); // where I am fetching object from DB with old values and construct the new object

        validatorService.validate( obj ); // which throws ValidationException

        return saveObject(obj);

But unfortunately even after the ValidationException was thrown. The values still get persisted in DB. How can I avoid this situtation.


You can evict the entity on ValidationException:

try {
    validatorService.validate( obj );
} catch (ValidationException e) {
    //Or with Hibernate API
    throw e;

Leave a Reply

Your email address will not be published. Required fields are marked *