9.4.4 Obtaining the entities from the database
By invoking the find method or getReference method of EntityManager, you can obtain the entities corresponding to the primary key specified in the argument, from the database.
The entities returned by the find method or getReference method of EntityManager use the persistence context of the transaction scope. Therefore, when the method is invoked within the transaction, the state changes to managed. Also, if the find method or getReference method is invoked outside the transaction, the state changes to detached.
The following table describes the state of entities obtained with the find operation or getReference operation.
Persistence context |
State of the obtained entity |
---|---|
Persistence context of the transaction scope (outside the transaction) |
detached |
Persistence context of the transaction scope (within the transaction) |
managed |
Extended persistence context (outside the transaction) |
managed |
Extended persistence context (within the transaction) |
managed |
Note that for Cosminexus JPA Provider, the following points differ from the JPA specifications in the find operation or getReference operation. Note that apart from these differences, there are no other functionality differences with the JPA specifications.
-
With the JPA specifications, in the getReference method of EntityManager, you can return Proxy to the entity corresponding to the primary key given in the argument and not in the actual instance. However, with Cosminexus JPA Provider, Lazy loading is not supported for @Basic. Therefore, with Cosminexus JPA Provider, entity instance is returned as the return value of getReference instead of Proxy.
For details on the supported range of Lazy loading for a relationship, see 9.4.5(2) Reading the entity information from the database.
-
With Cosminexus JPA Provider, if an entity that does not exist between the find method and getReference method is specified in the argument, a null value is returned with the find method. Also, EntityNotFoundException is thrown with the getReference method.