development

CrudRepository # findOne 메서드 누락

big-blog 2020. 9. 17. 08:26
반응형

CrudRepository # findOne 메서드 누락


내 프로젝트에서 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'
}

최신 정보:

이 방법이 다음으로 대체 된 것 같습니다. CrudRepository#findById


참조하시기 바랍니다 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(…)         ║
╚═════════════════════╩═══════════════════════╝

findById에 대한 정확한 대체하지 않습니다는 findOne, 그것은 반환 Optional대신에 null.

새로운 자바에 익숙하지 않아서 알아내는 데 약간의 시간이 걸렸지 만 이것은 findById동작을 findOne하나로 바꿉니다 .

return rep.findById(id).orElse(null);

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

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

A pragmatic transform

Old way:

Entity aThing = repository.findOne(1L);

New way:

Optional<Entity> aThing = repository.findById(1L);

참고URL : https://stackoverflow.com/questions/44101061/missing-crudrepositoryfindone-method

반응형