How can we check if an Object is in Persistent, Detached or Transient state in Hibernate?

We can use following methods to check the state of an object in Hibernate:

  • Persistent State: If call to EntityManager.contains(object) returns true, the object is in Persistent state.
  • Detached State: If the call to PersistenceUnitUtil.getIdentifier(object) returns identifier property then the object is in detached state.
  • Transient State: If call to PersistenceUnitUtil.getIdentifier(object) returns null then object is in Transient state.We can get access to PersistenceUnitUtil from the EntityManagerFactory in Hibernate.