如果保存功能不成功,Spring JPA 是否会抛出错误? [英] Does Spring JPA throw an error if save function is unsuccessful?

查看:52
本文介绍了如果保存功能不成功,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 Data Repositories 抛出 Spring 的 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 是否会抛出错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆