findById() and getOne() (now referenceById() in newer Spring Data versions) both retrieve an entity by its primary key, but they differ in when the database is actually hit -- findById() fetches eagerly and returns an Optional, while getOne() returns a lazy proxy that only queries the database when its fields are first accessed.
Setting a foreign key relationship, like assigning an order to a customer, only needs the customer's reference, so getOne(customerId) (or getReferenceById()) avoids a wasted SELECT, whereas displaying the customer's actual details requires findById() to eagerly load the real data.
// Eager fetch, safe existence check via Optional
Optional<Customer> customer = customerRepository.findById(id);
// Lazy proxy, defers the query until fields are accessed
Customer customerRef = customerRepository.getReferenceById(id);
order.setCustomer(customerRef);
orderRepository.save(order);A concise interview answer is
findById() eagerly queries the database and returns an Optional, so I can safely check whether the entity exists. getOne(), now getReferenceById(), returns a lazy proxy that defers the actual query until a field is touched, and throws EntityNotFoundException at that point if the row doesn't exist -- I use it when I just need a reference to set up a relationship without an unnecessary SELECT.
Last updated: 11 November 2026
Previous question: What is the use of @Modifying annotation?
Next question: What is the @Temporal annotation used for?