避免在handleBeforeSave中保存spring数据中的数据

时间:2015-10-07 20:07:21

标签: spring spring-data-mongodb spring-data-rest

我正在使用@RepositoryRestResource用于Mongo存储库。 我想做一些预保存检查,如果这些检查不符合要求,我想放弃保存操作。

我试过了:

    @HandleBeforeSave
    public void handleBeforeSave(CallLog callLog)
            throws InternalServerException {

    CallLog log = callRepository.findOne(callLog.getConferenceId());
    if (log != null
            && (callLog.getStatus().equals(Constants.CALL_IN_PROGRESS) || callLog
                    .getStatus().equals(Constants.CALL_DROPPED))) {
        if (callLog.getStatus().equals(Constants.CALL_DROPPED)) {
            User user = userRepository.findOne(callLog.getReceiverId());
            user.setStatus(Constants.USER_STATUS_IDLE);
            userRepository.save(user);
        }

        throw new InternalServerException(
                "This call has already been received");
    } else {
        User user = userRepository.findOne(callLog.getReceiverId());
        user.setStatus(Constants.USER_STATUS_BUSY);
        userRepository.save(user);
    }

}

但抛出异常,实际上并没有放弃保存调用。还有其他办法吗?

1 个答案:

答案 0 :(得分:0)