I have the following scenario:
- There's a user repository which can save/update a user
- I want to validate the user's firstname/lastname (perform some string operations) before saving/updating
- If the user's firstname/lastname is < 2 character after having performed the operations, a checked custom
UnacceptableStringValueException
should be thrown
Note that many services access the user repository, so it's cumbersome to implement new logic directly before saving.
What I tried to do in order to perform the validation just before saving/updating without having to implement this for every save/update call explicitly, is to use a JPA listener. Using @PrePersist
and @PreUpdate
I achieved the goal of performing the validation before each save/update. However there are two problems with this solution:
- The checked exception is not enforced on the methods which save/update a user and the exception gets automatically wrapped as a
RuntimeException
via the listener - When trying to use AOP to catch said
RuntimeException
and unpack the original checked exception, I keep gettingorg.springframework.transaction.UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only
Note that a simple service layer performing the check before saving/updating the user won't solve my use case because there's a user role repository which could also save/update a user (@ManyToOne(optional = false, cascade = CascadeType.PERSIST)
)
What's a good way to solve the constraints described in the scenario on top?
I found a somewhat viable solution by fixing the org.springframework.transaction.UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only
error.
The problem was, that the checked exception caused an unexpected rollback, in order to fix this, I added my custom checked exception to the exceptions which can be rolled back like this: @Transactional(rollbackFor = {UnacceptableStringValueException.class})