CrudRepository.save在Spring Data 1.13.18.RELEASE中是否返回null?

huangapple go评论107阅读模式
英文:

Does CrudRepository.save return null for spring data 1.13.18.RELEASE

问题

我了解到对于当前版本(2.x.x)的spring-data-commons,在这里有确切说明,CrudRepository.save()方法的返回值将永远不会为null。然而,对于旧版本,特别是1.13.18.RELEASE呢?

如果旧版本确保返回非null值,那么下面这段代码就变得无用,对吗?

Entity savedEntity = someRepository.save(entity);
if (savedEntity == null) {
    // 抛出某个已检查异常,说明保存失败了…
}

如果旧版本不保证返回非null值,那么什么情况会导致返回null值呢?

更新
由于我的问题涉及到CrudRepository的实现,需要指出我正在使用spring-data-jpa1.11.18.RELEASE版本。我想了解这个版本中save函数的行为。

英文:

I understand that for the current version(2.x.x) of spring-data-commons it is guaranteed that the return value of CrudRepository.save() will never be null. However, what about the older versions, specifically 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?

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. I want to know about the behaviour of save function for this version.

答案1

得分: 1

相同的问题在这里。
尽管它不应该返回 null,在我的项目中,当我更新现有实体时,我会得到一个 null 值(静默失败)。
如果我创建一个新的实体,它就可以工作。我还尝试实现了 Persistable 接口,但问题仍然存在。
我正在使用 Spring Data + R2DBC + Java 15。

英文:

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.
If I create a new entity it works. I tried also implementing the Persistable interface, but still the same issue.
I am using Spring Data + R2DBC + Java 15.

答案2

得分: 0

根据实现,它将始终返回一个值或抛出异常

更新,该实现适用于1.11.18.RELEASE。来自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);
    }
}
英文:

According to implementation, it will always return a value or throw an exception

Update the implementation is for 1.11.18.RELEASE. From GitHub repository

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

huangapple
  • 本文由 发表于 2020年4月4日 21:41:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61028977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定