I don't know how Spring implemented this in the past, but at least currently it's not correct that they are the same (just alias).
(This part above is incorrect from the original accepted)
Spring JPA is a layer on top of JPA. Therefore each of these operations is mapped to a Standard JPA operation:
findById(id)
-> [on JPA] entityManager.find
Whilst,
getOne(id)
-> [JPA] entityManager.getReference
So what's the difference on JPA then?
entityManager.find
goes directly to the DB, executes the query and returns all mapped columns to the memory. Pretty straightforward.
entityManager.getReference
is less used (it's less known). It's sort of a lazy find.
That is, it doesn't go directly to the DB. It only goes when the data is used. Its main target is when you just want a reference to some entity, but you won't use the entity (the values of the columns).
For instance:
class Customer {
Long id;
... many other fields
}
class Order {
Customer customer;
// ... other
}
You want to save a new Order
:
var order = new Order();
// Opt 1 (find): goes directly to DB and loads everything from this customer. Even tough we don't need it all.
// order.setCustomer(repo.findById(123L));
// ...
// Opt 2 (get): Won't go to DB at this point - but rather get a reference to a specific id
// When saving, may throw exception if customer with given id doesn't exist
order.setCustomer(repo.getOne(123L));
orderRepo.saveOrUpdate(order);
You can check a whole article explaining the diff, and better about getReference
here: https://vladmihalcea.com/entitymanager-find-getreference-jpa/.
The article isn't about Spring, but again, Spring JPA just follows JPA.