Missing CrudRepository#findOne method

JavaSpringSpring BootSpring DataSpring Data-Jpa

Java Problem Overview


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-data-jpa'

    runtime 'com.h2database:h2:1.4.194'
}

UPDATE:

Seems that this method has been replaced with CrudRepository#findById

Java Solutions


Solution 1 - Java

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(…)         ║
╚═════════════════════╩═══════════════════════╝

Solution 2 - Java

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);

Solution 3 - Java

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); 
    } 
} 

Solution 4 - Java

A pragmatic transform

Old way:

Entity aThing = repository.findOne(1L);

New way:

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

Solution 5 - Java

An other way more. Add a @Query. Even if I would prefer to consume the Optional:

@Repository
public interface AccountRepository extends JpaRepository<AccountEntity, Long> {

    @Query("SELECT a FROM AccountEntity a WHERE a.id =? 1")
	public AccountEntity findOne(Long id);

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrii AbramovView Question on Stackoverflow
Solution 1 - JavaSean CarrollView Answer on Stackoverflow
Solution 2 - JavaTinus TateView Answer on Stackoverflow
Solution 3 - JavaasharioView Answer on Stackoverflow
Solution 4 - JavaJames GrahamView Answer on Stackoverflow
Solution 5 - JavatimguyView Answer on Stackoverflow