Skip to main content
InterviewHubPRO
Section Overview
Q21

Question 21 of 50

Part 2: Persistence, Transactions & Security (11 - 24)

42%
Spring Boot & Microservices (50 Qs)•Part 2: Persistence, Transactions & Security (11 - 24)

What is the difference between `findById()` and `getReferenceById()` in Spring Data JPA?

junior LevelState ManagementSecurity & AuthDatabases & SQL

In Simple Words:

  • findById(): Immediately issues an SQL SELECT to fetch the entity and initialize all eager properties. Returns Optional<T>.
  • getReferenceById(): Returns a lazy Hibernate proxy without hitting the database. The SQL query is only executed when a property (other than the ID) is accessed. Useful when setting a foreign key relation without needing to load the entire referenced entity.