JPA EntityManager find() method

In this post, we will see JPA EntityManager find() method using Spring Boot.

The find() method used to retrieve an entity defined as below in the EntityManager interface.

T find(Class<T> entityClass, Object primaryKey) –  Returns entity for the given primary key.

It returns null if entity is not found in the database.

Note – The EntityManger interface is available in javax.persistence package. There are four overloaded versions of find() method which we will see at the end of the post.

Let’s see how to use EntityManager find() method.

First, we need to create a reference of the EntityManger.

@PersistenceContext
private EntityManager entityManger;

Using this we can call find() method.

 

Note – Implementation of EntityManger find() has been given in Hibernate’s SessionImpl.java as below.

@Override
public <T> T find(Class<T> entityClass, Object primaryKey) {
return find( entityClass, primaryKey, null, null );
}

 

Note – Just an additional note Crudrepository’s finById() method internally uses EntityManger find() method. See an example here.

Let’s see a complete example of JPA EntityManager find() method using Spring Boot and Oracle.

Open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)’ click on next.  Fill all details(GroupId – entitymanagerfind, ArtifactId – entitymanagerfind and name – entitymanagerfind) and click on finish. Keep packaging as the jar.

Modify pom.xml

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK.

If you see any error for oracle dependency then follow these steps.

Directory structure –

Spring Data JPA Is and Equals

 

Let’s see about JPA EntityManager find() method.

Student.java

StudentController.java

 

Note – See more details about @Controller and RestController here.

StudentRepository.java – interface

StudentService.java – interface

Note – See here more about @Component, @Controller, @Service and @Repository annotations here.

StudentServiceImpl.java

SpringMain.java

Note – See more details about @ComponentScan here.

JpaConfig.java

Note – See more details about @Configuration annotations here.

application.properties

Let’s run the SpringMain class(run as java application).

Perform save operation first using below REST API.

http://localhost:9091/student/save

 

Response Data.

 

http://localhost:9091/student/find/1

JPA EntityManager find() method

Query genrated for above operation using JPA Entitymanger find() method.

Hibernate:
select
student0_.id as id1_0_0_,
student0_.name as name2_0_0_,
student0_.roll_number as roll_number3_0_0_,
student0_.university as university4_0_0_
from
student student0_
where
student0_.id=?

There are four overloaded versions of find() method defined in the EntityManger interface.

T find(Class<T> entityClass, Object primaryKey) –  Returns entity for given primary key.
T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode) – Returns entity for given primary key and lock type.
T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode, Map<String,Object> properties) – Returns entity for given primary key, lock type and using the specified properties
T find(Class<T> entityClass, Object primaryKey, Map<String,Object> properties) – Returns entity for given primary key, lock type and using the specified properties.

 

That’s all about JPA EntityManager find() method using Spring Boot.

Spring Data JPA examples.

Other Spring Data JPA and Hibernate tutorials.

EntityManger docs.

Summary – We have seen about JPA EntityManager find() method. We also covered how to retreive a record from Database.

Top