I have the following scenario:
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:
RuntimeException
via the listener RuntimeException
and unpack the original checked exception, I keep getting org.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})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.