How to avoid automatic update of an object in Hibernate ValidationException
I have a method spring
: where I check the object after building the object that was previously fetched from the DB.
@Transactional(rollbackFor={ValidationException.class})
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 it was thrown out ValidationException
. The values โโare still stored in the database. How can I avoid this situation.
source to share
I have never used spring, but if it is the same as EJB then transactions are only rolled back when thrown exceptions are thrown.
When java.lang.Exception is thrown from an EJB method, you obviously need to declare it in the method signature with the therows keyword. What happens in this case is as follows: 1) the transaction ends 2) the exception is returned to the client
Taken from this link
source to share