本文介绍了如果保存功能失败,Spring JPA是否会引发错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在尝试将对象保存到数据库时Spring JPA是否引发错误以及错误类型. JpaRepository.java说要查看org.springframework.data.repository.CrudRepository#save.但是,我似乎找不到任何类型的错误(如果有的话).如果未引发任何错误,我该如何检查保存是否成功?

I would like to know if and what type of error is thrown by Spring JPA when trying to save an object to a database. The JpaRepository.java says to look at org.springframework.data.repository.CrudRepository#save. However, I can not seem to find what type of error is thrown, if any at all. If no error is thrown, how do I go about checking if the save was successful?

任何见识将不胜感激.

推荐答案

Spring数据存储库抛出Spring的DataAccessException,这些DataAccessException的结构使您可以根据某些错误情况捕获异常.例如.抛出DataIntegrityViolationException表示违反外键.

Spring Data Repositories throw Spring's DataAccessExceptions that are structured to allow you to catch exceptions based on certain error scenarios. E.g. a DataIntegrityViolationException is thrown to indicate foreign key violations.

原始异常包含在抛出的异常内,以便您可以根据需要进行处理.通过抛出与持久性技术无关的异常,存储库客户端不会受到技术特定异常的污染(例如,Hibernate异常,JPA异常,MongoDB异常),以便您可以在不破坏客户端的情况下交换存储库实现.

The original exception is contained inside the exception being thrown so that you can get to it if you need to. By throwing a persistence technology agnostic exception, the repository clients don't get polluted with technology specific exceptions (e.g. Hibernate ones, JPA ones, MongoDB ones) so that you can exchange the repository implementation without breaking clients.

这篇关于如果保存功能失败,Spring JPA是否会引发错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 19:45