Does Spring JPA throw an error if save function is unsuccessful?

前端 未结 3 1875
暗喜
暗喜 2021-01-18 13:45

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

相关标签:
3条回答
  • 2021-01-18 14:12

    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.

    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.

    0 讨论(0)
  • 2021-01-18 14:14

    CrudRepostiroy.save have return type the saved entity

    <S extends T> S save(S entity)
    Saves a given entity. Use the returned instance for further operations as the save operation might have changed the entity instance completely.
    Parameters:
    entity -
    Returns:
    the saved entity
    
    0 讨论(0)
  • 2021-01-18 14:15

    It should throw a org.springframework.orm.jpa.JpaSystemException. Also, CrudRepostiroy.save returns you a new instance of the entity you gave it, if you get one back, that's a good sign that it saved.

    0 讨论(0)
提交回复
热议问题