缺少CrudRepository#findOne方法

And*_*mov 89 java spring spring-data spring-data-jpa spring-boot

我在我的项目中使用Spring 5.直到今天还有可用的方法CrudRepository#findOne.

但下载最新快照后,它突然消失了!有没有提到该方法现在不可用?

我的依赖列表:

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-validation'
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtime 'org.springframework.boot:spring-boot-devtools'

    runtime 'com.h2database:h2:1.4.194'
    compile 'org.projectlombok:lombok:1.16.14'
    compile 'org.modelmapper:modelmapper:0.7.5'


    testCompile 'org.springframework.boot:spring-boot-starter-test'

    testCompile 'org.codehaus.groovy:groovy-all:2.4.10'

    testCompile 'cglib:cglib:3.2.5'
    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
}
Run Code Online (Sandbox Code Playgroud)

更新:

似乎这个方法已被替换 CrudRepository#findById

Sea*_*oll 132

请参阅与此提交相关联的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(…)         ?
???????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

  • 不确定这是否是任何迁移指南,但您可以在Kay发布列车维基(https://github.com/spring-projects/spring-data-commons/wiki/Release-Train-Kay)中找到它的参考作为Spring Data Commons changelog(https://docs.spring.io/spring-data/commons/docs/current/changelog.txt) (2认同)

Tin*_*ate 99

请注意,findById它不是一个确切的替代品findOne,它返回一个Optional而不是null.

我不太熟悉新的java东西,花了一些时间来弄清楚,但这会把findById行为变成findOne一个:

return rep.findById(id).orElse(null);
Run Code Online (Sandbox Code Playgroud)

  • @GabiM如果您控制下游的所有方法,这将是很好的.即使您确实控制了下游的所有内容,并且您的项目不是某些其他第三方项目的依赖项,如果下游的方法代码为null(如果创建,如果不存在,或者如果缺少则执行某些逻辑),那么您有修复它们. (5认同)

ash*_*rio 28

我们有几百个旧findOne()方法的用法.我们最终创建了以下中间接口,并让我们的存储库扩展它而不是JpaRepository直接扩展,而不是开始使用庞大的重构器.

@NoRepositoryBean
public interface BaseJpaRepository<T, ID> extends JpaRepository<T, ID> { 
    default T findOne(ID id) { 
        return (T) findById(id).orElse(null); 
    } 
} 
Run Code Online (Sandbox Code Playgroud)


nhu*_*uvy 6

务实的转变

旧方法:

Entity aThing = repository.findOne(1L);
Run Code Online (Sandbox Code Playgroud)

新的方法:

Optional<Entity> aThing = repository.findById(1L);
Run Code Online (Sandbox Code Playgroud)