Missing CrudRepository#findOne method

2019-01-31 11:27发布

I am using Spring 5 in my project. Until today there was available method 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-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'
}

UPDATE:

Seems that this method has been replaced with CrudRepository#findById

4条回答
迷人小祖宗
2楼-- · 2019-01-31 11:31

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); 
    } 
} 
查看更多
Viruses.
3楼-- · 2019-01-31 11:35

Please see DATACMNS-944 which is associated to this commit which has the following renames

╔═════════════════════╦═══════════════════════╗
║      Old name       ║       New name        ║
╠═════════════════════╬═══════════════════════╣
║ findOne(…)          ║ findById(…)           ║
╠═════════════════════╬═══════════════════════╣
║ save(Iterable)      ║ saveAll(Iterable)     ║
╠═════════════════════╬═══════════════════════╣
║ findAll(Iterable)   ║ findAllById(…)        ║
╠═════════════════════╬═══════════════════════╣
║ delete(ID)          ║ deleteById(ID)        ║
╠═════════════════════╬═══════════════════════╣
║ delete(Iterable)    ║ deleteAll(Iterable)   ║
╠═════════════════════╬═══════════════════════╣
║ exists()            ║ existsById(…)         ║
╚═════════════════════╩═══════════════════════╝
查看更多
Evening l夕情丶
4楼-- · 2019-01-31 11:38

A pragmatic transform

Old way:

Entity aThing = repository.findOne(1L);

New way:

Optional<Entity> aThing = repository.findById(1L);
查看更多
看我几分像从前
5楼-- · 2019-01-31 11:40

Note that findById is not an exact replacement for findOne, it returns an Optional instead of 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:

return rep.findById(id).orElse(null);
查看更多
登录 后发表回答