简体   繁体   中英

How to validate before saving and throw checked exception

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 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM