我知道,对于当前版本的2.x.x,spring-data-commons的返回值永远不会为null的有保证。但是,更早的版本,特别是1.13.18.RELEASE呢?
如果旧版本确实保证返回非空值,那么它就会使下面的代码变得无用,对吗?
Entity savedEntity = someRepository.save(entity);
if (savedEntity == null) {
// throw some checked exception stating that the save failed..
}如果旧版本不能保证返回一个非空值,那么什么情况会导致返回一个空值?
Update:由于我的问题与CrudRepository的实现有关,所以必须指出,我正在使用1.11.18.RELEASE of spring-data-jpa。我想知道这个版本的保存函数的行为。
发布于 2021-03-11 03:48:35
这里也有同样的问题。即使它永远不应该返回null,但在我的项目中,当我更新一个现有实体时,我会得到一个空值(沉默失败)。如果我创建一个新的实体,它就能工作。我也尝试过实现Persistable接口,但仍然存在相同的问题。我正在使用Spring + R2DBC + Java 15。
发布于 2020-04-04 13:53:28
根据实现,它总是返回一个值或抛出一个异常
更新实现是针对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);
}
}https://stackoverflow.com/questions/61028977
复制相似问题