Java 缺少 CrudRepository#findOne 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44101061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Missing CrudRepository#findOne method
提问by Andrii Abramov
I am using Spring 5 in my project. Until today there was available method CrudRepository#findOne
.
我在我的项目中使用 Spring 5。直到今天才有可用的方法CrudRepository#findOne
。
But after downloading latest snapshot it suddenly disappeared! Is there any reference that the method is not available now?
但是下载最新的快照后它突然消失了!是否有任何参考该方法现在不可用?
My dependency list:
我的依赖列表:
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
runtime 'com.h2database:h2:1.4.194'
}
UPDATE:
更新:
Seems that this method has been replaced with CrudRepository#findById
似乎这种方法已被替换为 CrudRepository#findById
采纳答案by Sean Carroll
Please see DATACMNS-944which is associated to this commitwhich has the following renames
请参阅与此提交相关联的DATACMNS-944,该提交具有以下重命名
╔═════════════════════╦═══════════════════════╗
║ Old name ║ New name ║
╠═════════════════════╬═══════════════════════╣
║ findOne(…) ║ findById(…) ║
╠═════════════════════╬═══════════════════════╣
║ save(Iterable) ║ saveAll(Iterable) ║
╠═════════════════════╬═══════════════════════╣
║ findAll(Iterable) ║ findAllById(…) ║
╠═════════════════════╬═══════════════════════╣
║ delete(ID) ║ deleteById(ID) ║
╠═════════════════════╬═══════════════════════╣
║ delete(Iterable) ║ deleteAll(Iterable) ║
╠═════════════════════╬═══════════════════════╣
║ exists() ║ existsById(…) ║
╚═════════════════════╩═══════════════════════╝
回答by Do Nhu Vy
A pragmatic transform
务实的转变
Old way:
旧方式:
Entity aThing = repository.findOne(1L);
New way:
新方法:
Optional<Entity> aThing = repository.findById(1L);
回答by Tinus Tate
Note that findById
is not an exact replacement for findOne
, it returns an Optional
instead of null
.
请注意,这findById
不是 的精确替代品findOne
,它返回一个Optional
而不是null
。
Being not very familiar with new java things it took me a little while to figure out, but this turns the findById
behavior into the findOne
one:
由于对新的 Java 事物不是很熟悉,我花了一些时间才弄明白,但这将findById
行为变成了findOne
这样:
return rep.findById(id).orElse(null);
回答by ashario
We had many hundreds of usages of the old findOne()
method. Rather than embark on a mammoth refactor, we ended up creating the following intermediary interface and had our repositories extend it instead of extending JpaRepository
directly
我们有数百种旧findOne()
方法的用法。我们没有进行大规模的重构,而是创建了以下中间接口,并让我们的存储库扩展它而不是JpaRepository
直接扩展
@NoRepositoryBean
public interface BaseJpaRepository<T, ID> extends JpaRepository<T, ID> {
default T findOne(ID id) {
return (T) findById(id).orElse(null);
}
}