JPA EntityManager persist() and merge() method.

In this post, we will see the JPA EntityManager persist() and merge() method.

Difference between persist() and merge() method.

1.  The persist() method is used to create or save a new entity in the database. if we try to update an existing record using persist() method it will throw EntityExistsException. Using merge() method we can create/save a new record as well as we can update an existing record.

2. The return type of the persist() method is void whereas the merge() method returns a managed entity.

public void persist(Object entity);

public T merge(T entity);

3. Sample example of persist() and merge() method.

 

4. Since persist() method used to create new record only it will fire INSERT SQL statement whereas in case of merge() method INSERT SQL statement as well UPDATE SQL statement will get executed.

The query generated while saving a new Student entity using persist() method.

Hibernate:
insert
into
student
(name, roll_number, university, id)
values
(?, ?, ?, ?)

 

The query generated while saving a new Student entity using merge() method.

Hibernate:
insert
into
student
(name, roll_number, university, id)
values
(?, ?, ?, ?)

 

The query generated while updating an existing Student entity using merge() method.

Hibernate:
update
student
set
name=?,
roll_number=?,
university=?
where
id=?

 

Using persist() and merge() method ServiceImpl Example.

 

First, we need to create an EntityManger reference.

@PersistenceContext
private EntityManager entityManger;

Using this we can call persist() method.

 

Note – EntityManger persist() and merge() method Implementation in Hibernate.

@Override
public void persist(Object object) throws HibernateException {
checkOpen();
firePersist( new PersistEvent( null, object, this ) );
}

 

@Override
public Object merge(Object object) throws HibernateException {
checkOpen();
return fireMerge( new MergeEvent( null, object, this ));
}

 

 

Example of JPA EntityManager persist() and merge() methods 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 – entitymanager, ArtifactId – entitymanager and name – entitymanager) 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.

Let’s see the directory structure of JPA EntityManager persist() method example as below.

Directory structure –

JPA EntityManager persist() Example

 

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

Student.java

StudentController.java

We have seen controller and serviceimpl, interface for JPA EntityManager persist() and merge() method Example.

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

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.

application.properties

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

 

Testing of example using postman.

Perform save operation first using below REST API.

POST – http://localhost:9091/student/save

 

Response Data.

 

Perform update operation.

PUT – http://localhost:9091/student/update/

Request data.

Response Data.

 

 

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

JPA EntityManager persist() method

 

 

 

 

 

 

 

 

That’s all about JPA EntityManager persist() and merge() methods.

You may like.

Other Hibernate and JPA tutorial.

 

EntityManger docs.

Summary- We have seen JPA EntityManager persist() and merge(). Generally The merge() method be used for update() operation and persist() method is used to creating/save a new entity. The EntityManger interface is available in javax.persistence package. Using merge() method we can create a  new entity. But JPA EntityManger provides persist() method to create an entity. The merge() throws IllegalArgumentException if an instance is not an entity or is a removed entity. In Spring Data JPA CrudRepository save() method internally uses EntityManager merge() method to update an entity. See an example

 

 

Top