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 SQLSELECTto fetch the entity and initialize all eager properties. ReturnsOptional<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.