Deleting Entities with Hibernate

Learn to delete a single entity or a list of entities matching some criteria using hibernate native APIs and Jakarta persistence APIs.

1. Using Session.remove()

The most straightforward support for deleting an Entity comes with Session interface and its delete() method.

EmployeeEntity emp = session.get(EmployeeEntity.class, 1L);
session.remove(emp);

The session.remove() method can delete an entity in a detached state also.

EmployeeEntity emp = session.get(EmployeeEntity.class, 1L);
session.evict(emp);

//Works just fine
session.remove(emp);

Session interface has one more similar method delete(). It has been deprecated since hibernate version 6.0 and using remove() is recommended.

2. Using EntityManager.delete()

The delete() API from EntityManager works almost similar to the previously discussed Session.remove().

EmployeeEntity employee = em.find(EmployeeEntity.class, 1L);

em.remove(employee);

There is one pre-condition that the entity must be in a persistent state. The delete() operation can not delete a detached entity. In this case, this API will throw IllegalArgumentException.

EmployeeEntity employee = em.find(EmployeeEntity.class, 1L);

em.detach(employee);

em.remove(employee);

The above code is trying to execute a detached entity so it will throw the exception.

java.lang.IllegalArgumentException: Removing a detached instance com.howtodoinjava.demo.entity.EmployeeEntity#1

3. Deleting List of Entities

The EntityManager.remove() and Session.remove() methods are a better fit when we want to delete a single Entity but also very inefficient if we need to remove a list of entities.

For each remove() call, hibernate loads the entity, perform the lifecycle transition to REMOVED and trigger the SQL DELETE operation. Executing N different SQL DELETE queries for N entities will lead to very inefficient performance. It’s often better to remove such a list of entities with a JPQL query.

We should flush all the pending changes to the database before executing the query so that we are making changes to the latest data. And we also minimize the risk of stale first-level cache.

//Clear persistence context beforehand
em.flush();
em.clear();

//Get list of Ids
List<Long> ids = getIdsToDelete();
  
//Remove all entities
Query query = em.createQuery("DELETE EmployeeEntity e WHERE id IN (:ids)");
query.setParameter("ids", ids);
query.executeUpdate();

4. Conclusion

In this tutorial, we learned to delete a single entity as well as a list of entities. W learned to delete a single entity using Session and Entitymanager interface and to use the JPQL query to delete a list of entities for better performance.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode