java - How to avoid automatic entity update on Hibernate ValidationException -
i having spring
method: validating entity after constructing of object, fetched db.
@transactional(rollbackfor={validationexception.class}) public object approve(command command) throws validationexception { object obj = merger.mergewithexistingobject(command); // fetching object db old values , construct new object validatorservice.validate( obj ); // throws validationexception return saveobject(obj); }
but unfortunately after validationexception
thrown. values still persisted in db. how can avoid situtation.
you can evict entity on validationexception
:
try { validatorservice.validate( obj ); } catch (validationexception e) { entitymanager.detach(obj); //or hibernate api //session.evict(obj); throw e; }
Comments
Post a Comment