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)
Tin*_*ate 99
请注意,findById它不是一个确切的替代品findOne,它返回一个Optional而不是null.
我不太熟悉新的java东西,花了一些时间来弄清楚,但这会把findById行为变成findOne一个:
return rep.findById(id).orElse(null);
Run Code Online (Sandbox Code Playgroud)
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)
务实的转变
旧方法:
Entity aThing = repository.findOne(1L);
Run Code Online (Sandbox Code Playgroud)
新的方法:
Optional<Entity> aThing = repository.findById(1L);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39048 次 |
| 最近记录: |