how do you handle and parse JPA persistence exceptions to provide meaningfull message to user

后端 未结 5 1677
慢半拍i
慢半拍i 2020-12-30 23:13

I am fairly new to JPA and want to find best practices when handling persistence exceptions from JPA for things like say, unique constraint violations which can be

5条回答
  •  盖世英雄少女心
    2020-12-30 23:59

    I'm doing something similar in my DB service layer to figure out whether the exception was caused by a conflicting constraint or general DB failure:

    try {
    ....
    } catch (final PersistenceException e) {
        final Throwable cause = e.getCause();
        if (cause instanceof MySQLIntegrityConstraintViolationException) {
            throw new ConflictException(cause);
        }
        throw new ServiceException(e);
    }
    

提交回复
热议问题