How are persistent entities in JPA retrieved?
Here are 2 possibilities besides other query calls:
- Find() is called on the EntityManager and the method needs 2 parameters:
Example:
EntityManagerFactory ef = Persistence.createEntityManagerFactory("myapp") ;
EntityManager em = ef.createEntityManager();
....
Client k = em.find(Client.class, 70992);
-- Find by primary key. Search for an entity of the specified class and primary key. If the entity instance is contained in the persistence context, it is returned from there.
- GetReference() is used similarly.
Client k = em.getReference(Client.class, 70922);
What is the difference between both methods?
Find() delivers the entity from the cache of the persistence context or if he is not there, it will be loaded from the database.
GetReference() does not load the entity immediately. A proxy( a certain object, a so called "deputy" with enriched methods for loading the actual entity) is returned. So it is a realisation with help of LazyLoading.
Only if the attributes of the proxy or other persistence methods are needed/called the proxy interacts and loads the actual entity from the database.
When should one use which method?
The usage of find() should take priority over query methods, because find() can return already loaded entities from the cache of the persistence context.
If you do know, that an entity is need later on, the usage of getReference() is a good choice.
Here are 2 possibilities besides other query calls:
- find()
- getReference()
- Find() is called on the EntityManager and the method needs 2 parameters:
- Type of the wanted entity
- Identity: Id of the wanted entity
Example:
EntityManagerFactory ef = Persistence.createEntityManagerFactory("myapp") ;
EntityManager em = ef.createEntityManager();
....
Client k = em.find(Client.class, 70992);
-- Find by primary key. Search for an entity of the specified class and primary key. If the entity instance is contained in the persistence context, it is returned from there.
- GetReference() is used similarly.
Client k = em.getReference(Client.class, 70922);
If the entity for the stated id is not known in the persistence context, an EntityNotFoundException() is thrown.
--Get an instance, whose state may be lazily fetched. If the requested instance does not exist in the database, the EntityNotFoundException
is thrown when the instance state is first accessed. (The persistence provider runtime is permitted to throw the EntityNotFoundException
when getReference
is called.) The application should not expect that the instance state will be available upon detachment, unless it was accessed by the application while the entity manager was open.
What is the difference between both methods?
Find() delivers the entity from the cache of the persistence context or if he is not there, it will be loaded from the database.
GetReference() does not load the entity immediately. A proxy( a certain object, a so called "deputy" with enriched methods for loading the actual entity) is returned. So it is a realisation with help of LazyLoading.
Only if the attributes of the proxy or other persistence methods are needed/called the proxy interacts and loads the actual entity from the database.
When should one use which method?
The usage of find() should take priority over query methods, because find() can return already loaded entities from the cache of the persistence context.
If you do know, that an entity is need later on, the usage of getReference() is a good choice.