I know that transient instance means the instance is newly created and its corresponding row does not exist in the database where as detached instance has corresponding entr
In order to check if object e is in :-
Persistence Context :- EntityManager.contains(e) should return true.
Detached State : PersistenceUnitUtil.getIdentifier(e) returns the value of the entity’s identifier property.
Transient State :- PersistenceUnitUtil.getIdentifier(e) returns null
You can get to the PersistenceUnitUtil from the EntityManagerFactory.
There are two issues to look out for. First, be aware that the identifier value may not be assigned and available until the persistence context is flushed. Second, Hibernate (unlike some other JPA providers) never returns null from Persistence- UnitUtil#getIdentifier() if your identifier property is a primitive (a long and not a Long).