An object that hibernate/JPA can persist in the database and retrieve it back whenever needed is called an Entity. JPA manages various states of these entities. As you learned in the basics of Entity classes, they are annotated with @Entity - javax.persistence.Entity.

Basically, there are 4 states of an Object in JPA and these are New (Transient), Persistent (Managed), Detached (Unmanaged) and Removed (deleted).

Entity instance states

1. New (Transient) state

An object that is newly created and has never been associated with JPA Persistence Context (hibernate session) is considered to be in the New (Transient) state. The data of objects in this state is not stored in the database.

Example, student object is in New/Transient state.

Student student = new Student("[email protected]");

2. Persistent (JPA managed) state

An Object that is associated with persistence context (hibernate session) are in Persistent state. Any changes made to objects in this state are automatically propagated to databases without manually invoking persist/merge/remove.

The example below will be clear to understand this.

Student student = new Student("[email protected]");

// Make changes and see if the data is updated automatically
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
entityManager.persist(student);
//purposely made changes - did not manually update
student.setEmail("[email protected]");
Long persistedId = student.getId();
tx.commit();
entityManager.close();

The object changes will be auto migrated at tx.commit(). To verify, use the below code. You will not need to use merge().

// Test if the email address was updated
entityManager = entityManagerFactory.createEntityManager();
tx = entityManager.getTransaction();
tx.begin();
student = entityManager.find(Student.class, persistedId);
tx.commit();
entityManager.close();
System.out.println("Persisted Student: " + student);

3. Detached (unmanaged) state

An Object becomes detached when the currently running Persistence Context is closed. Any changes made to detached objects are no longer automatically propagated to the database.

Once tx.commit() is executed, the student object becomes detached.

tx.begin();
student = entityManager.find(Student.class, persistedId);
tx.commit();
entityManager.close();
System.out.println("Persisted Student: " + student);
student.setEmail("[email protected]");

Now, the latest changes made to student object does not get updated in the database. You need to make use of merge to reattach the changes.

Use merge(T entity) from javax.persistence.EntityManager to synchronise the changes made to a detached object with the database.

This code explains the use of merge().

//To reattach a detached object
student.setEmail("[email protected]");

entityManager = entityManagerFactory.createEntityManager();
tx = entityManager.getTransaction();
tx.begin();
student = entityManager.merge(student);
tx.commit();
entityManager.close();
System.out.println("Persisted Student: " + student);

4. Removed object state

As the name suggests, removed objects are deleted from the database. JPA provides entityManager.remove(object); method to remove an entity from the database.

NOTE: Only persistent objects can be removed in JPA, any attempt to delete a dettached object will cause java.lang.IllegalArgumentException: Removing a detached instance.

//Remove an Object from the database
entityManager = entityManagerFactory.createEntityManager();
tx = entityManager.getTransaction();
tx.begin();
student = entityManager.find(Student.class, persistedId);
entityManager.remove(student);
tx.commit();
entityManager.close();

Code Examples

Entity states code content
example01 – Contains the code for Entity states

I am hoping to have provided a good understanding of the different entity states in JPA. Let me know your feedback in the comments below. For further reading, you can take a look at the various methods exposed by Entity Manager in JPA 2.2.

By |Last Updated: August 24th, 2019|Categories: Hibernate, JPA|

Table of Contents