Jpa : findOne vs getOne vs findById

Kush Singh
1 min readJan 20, 2020

--

getOne(): returns a reference to the entity with the given identifier. getOne internally invokes EntityManager.getReference() method. this method will always return a proxy without hitting the database (lazily fetched). This method will throw EntityNotFoundException at the time of actual access if the requested entity does not exist in the database

findById():This method will actually hit the database and return the real object mapping to a row in the database. It is EAGER loaded operation that returns null if no record exists in database.

s.no,getOne,findById
1,Lazily loaded reference to target entity,Actually loads the entity for the given id
2,Throws EntityNotFoundException if actual object does not exist at the time of access invocation,Returns null if actual object corresponding to given Id does not exist
3,Better performance,An additional round-trip to database is required

--

--