심화/DB

[DB] existById, findById (업데이트 중..)

annovation 2025. 11. 10. 22:00

Spring Data JPA의 existById, findById 차이점

 

💡existsById

  • 전달받은 ID에 해당하는 엔티티가 존재하면 true, 없으면 false를 반환하는 메서드
  • 반환 타입 : boolean
  • 주요 목적 : 존재 여부만 간단히 확인

💡findById

  • 해당 ID의 엔티티(객체)를 조회해서 Optional<Entity>로 반환
  • 반환 타입 : Optional<Entity>
  • 주요 목적 : 엔티티 데이터 자체가 필요한 경우
  • 만약 데이터가 없으면 Optional.empty 반환

실행 쿼리

 

💡existsById

 

💡findById


출처

https://stackoverflow.com/questions/50853740/what-is-the-difference-between-findbyid-and-existsbyid-in-spring-data-2-0

 

What is the difference between findById() and existsById() in Spring Data 2.0?

Spring Data 2.0 JpaRepository: difference between findById() and existsById() Which scenario do we use findById() and existsById() methods?

stackoverflow.com

'심화 > DB' 카테고리의 다른 글

[DB] Redis 설치하기 (MacOS)  (0) 2026.02.26
[DB] 인메모리 저장소와 Redis 기초  (0) 2026.02.25
[DB] NoSQL 장/단점  (0) 2025.10.28
[DB] NoSQL 특징  (0) 2025.10.27
[DB] Spring Boot 초기 데이터 세팅 (업데이트 중..)  (2) 2025.10.21