emazzotta
emazzotta

Reputation: 2049

How to validate before saving and throw checked exception

I have the following scenario:

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:

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?

Upvotes: 4

Views: 1870

Answers (1)

emazzotta
emazzotta

Reputation: 2049

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})

Upvotes: 1

Related Questions