简体   繁体   English

CrudRepository.save 是否为 spring 数据 1.13.18.RELEASE 返回 null

[英]Does CrudRepository.save return null for spring data 1.13.18.RELEASE

I understand that for the current version( 2.xx ) of spring-data-commons it is guaranteed that the return value of CrudRepository.save() will never be null.我知道对于spring-data-commons的当前版本( 2.xx ),可以保证CrudRepository.save()的返回值永远不会是 null。 However, what about the older versions, specifically 1.13.18.RELEASE ?但是,旧版本,特别是1.13.18.RELEASE呢?

If the older version does guarantee return of non-null values then it renders the following piece of code useless, right?如果旧版本确实保证返回非空值,那么它会使下面的代码无用,对吧?

        Entity savedEntity = someRepository.save(entity);
        if (savedEntity == null) {
            // throw some checked exception stating that the save failed..
        }

And if the older version doesn't guarantee return of a non-null value, then what scenario would lead to the return of a null value?如果旧版本不保证返回非空值,那么什么情况会导致返回 null 值?

Update : Since my question is pertaining to the implementation of CrudRepository , it is important to point out that I am using 1.11.18.RELEASE of spring-data-jpa .更新:由于我的问题与CrudRepository的实施有关,重要的是要指出我正在使用1.11.18.RELEASEspring-data-jpa I want to know about the behaviour of save function for this version.我想了解此版本保存function 的行为。

According to implementation , it will always return a value or throw an exception根据实现,它总是会返回一个值或抛出一个异常。

Update the implementation is for 1.11.18.RELEASE .更新实现是针对1.11.18.RELEASE的。 From GitHub repository来自 GitHub 存储库

@Transactional
public <S extends T> S save(S entity) {
    if (this.entityInformation.isNew(entity)) {
        this.em.persist(entity);
        return entity;
    } else {
        return this.em.merge(entity);
    }
}

same issue here.同样的问题。 Even though it should never return null, in my project I get a null value (silent failure) when I update an existing entity.即使它永远不应该返回 null,但在我的项目中,当我更新现有实体时,我会得到一个 null 值(静默失败)。 If I create a new entity it works.如果我创建一个新实体,它就可以工作。 I tried also implementing the Persistable interface, but still the same issue.我也尝试实现 Persistable 接口,但仍然是同样的问题。 I am using Spring Data + R2DBC + Java 15.我正在使用 Spring 数据 + R2DBC + Java 15。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 CrudRepository.save(Entity) 使用 spring-data-jdbc 插入具有预定义 @ID 字段的实体 - Using CrudRepository.save(Entity) to insert an Entity with predefined @ID field using spring-data-jdbc CrudRepository.save() 似乎级联 - CrudRepository.save() appears to cascade Spring data CrudRepository (hibernate) findone return null - Spring data CrudRepository (hibernate) findone return null Springboot 2 CrudRepository.save总是抛出ConstraintViolationException - Springboot 2 CrudRepository.save always throws ConstraintViolationException JPA持久性CrudRepository.save()语法错误 - JPA persistence CrudRepository.save() syntax error 单元测试 CrudRepository.save() 的正确方法 - Correct way to unit test CrudRepository.save() 在CrudRepository(Spring Data Cassandra)中保存方法无法在日期类型字段中保存空值吗? - save method in CrudRepository(Spring Data Cassandra) can't save a null value in date type field? CrudRepository 返回 null - CrudRepository return null 为什么 CrudRepository.save() 在返回创建的 object 时忽略 FetchType.EAGER? - Why CrudRepository.save() ignore FetchType.EAGER when returning created object? Spring CrudRepository保存方法的自定义返回类型(动态投影) - Custom return type (dynamic projection) for Spring CrudRepository save method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM