使用JpaRepository时无法调用findOne()方法的问题
findOne() Method in JpaRepository for Your Personne Entity Hey there! Let's break down why you can't access the findOne() method and how to fix it quickly. First, let's recap your Personne entity code for context:
package com.example.entities; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @SuppressWarnings("serial") @Entity public class Personne implements Serializable { @Id @GeneratedValue private Long id; @Column(length = 80, name = "NOM") private String nom; @Column(length = 80, name = "PRENOM") private String prenom; @Column(length = 80, name = "AD...") // Assuming this is a truncated address field private String adresse; }
Why findOne() is Missing
The findOne() method was removed in Spring Data JPA 2.0+ (which ships with Spring Boot 2.x and above). The old findOne(ID) method that fetched an entity by ID was split into two distinct methods to make intent clearer:
findById(ID id): Returns anOptional<T>to explicitly handle cases where the entity might not exist (avoids null pointer exceptions).getOne(ID id): Returns a lazy-loaded proxy of the entity (throwsEntityNotFoundExceptionif the entity doesn't exist, and only hits the database when you access entity properties).
Solutions to Replace findOne()
1. Use findById() (Recommended)
Replace your existing findOne() call with findById(), and handle the Optional return type properly. Here's how:
Before (old code):
Personne personne = personneRepository.findOne(1L);
After (new code):
Optional<Personne> personneOptional = personneRepository.findById(1L); // Option 1: Get the entity or return null if not found Personne personne = personneOptional.orElse(null); // Option 2: Throw an exception if the entity doesn't exist (better for required entities) Personne personne = personneOptional.orElseThrow(() -> new RuntimeException("Personne not found with ID: 1L"));
2. Use getOne() for Lazy-Loaded Proxies
If you need a lazy-loaded proxy (e.g., for relationships where you don't want to fetch the entire entity immediately), use getOne():
Personne personne = personneRepository.getOne(1L);
⚠️ Note: This method will throw EntityNotFoundException if the entity doesn't exist, and the actual database query only runs when you access a property like personne.getNom().
3. (Not Recommended) Re-Add findOne() to Your Repository
If you really need to keep using the findOne() method name (for legacy code compatibility), you can define it explicitly in your repository interface:
import org.springframework.data.jpa.repository.JpaRepository; import com.example.entities.Personne; public interface PersonneRepository extends JpaRepository<Personne, Long> { // Custom findOne method to mimic the old behavior Personne findOne(Long id); }
This works because Spring Data JPA will automatically implement the method based on the name, but this approach isn't future-proof—stick to the official methods above for long-term compatibility.
Verify Your Spring Data JPA Version
To confirm which version you're using:
- For Maven: Check the
spring-data-jpadependency in yourpom.xml - For Gradle: Check the
spring-data-jpadependency in yourbuild.gradle
Spring Boot 2.x uses Spring Data JPA 2.0+, while Spring Boot 1.x uses the older 1.x version where findOne() still exists.
内容的提问来源于stack exchange,提问作者BMW83

