Is there a way to query the JPA EntityManager whether a given entity is detached? This SO post is discussing a similar issue but does not indicate a way to query the JPA EntityManager on the detachment status of an entity. I would prefer a JPA way, otherwise Hibernate-specific.
-
3Did you try [`EntityManager#contains(-)`](http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html#contains%28java.lang.Object%29)? – Piotr Nowicki Oct 30 '12 at 09:13
-
Yeah, that seems to work. I was looking for something more explicit but the JavaDoc is clear: "Check if the instance is a managed entity instance belonging to the current persistence context." – Marcus Junius Brutus Oct 30 '12 at 10:06
-
Great, so just to keep things clean I've added this as an answer. – Piotr Nowicki Oct 30 '12 at 14:30
2 Answers
To check if the given entity is managed by the current PersistenceContext
you can use the EntityManager#contains(Object entity)
.

- 17,914
- 8
- 63
- 82
-
36-1 This is not a correct answer to the question. Reason: `contains()` will indeed return false if the entity is detached **but it will return false in other cases as well**. By spec, `contains()` returns if the given entity is managed. Detached entities are only one of several reasons that an entity is considered unmanaged. – MRalwasser Apr 29 '15 at 15:19
-
As @MRalwasser said: Is this still the case on Hibernate 5? Or ist this changed since the answer was posted? And in which version? – Pwnstar Mar 06 '18 at 07:59
Piotr Nowicki's answer provides a way of determining whether an entity is managed. To find out whether an entity has been detached, we'd need to know whether it was previously managed (i.e. came from the database, e.g. by being persisted or obtained from a find
operation). Hibernate doesn't provide an "entity state history" so the short answer is there isn't a 100% reliable way of doing this but the following workaround should be sufficient in most cases:
public boolean isDetached(Entity entity) {
return entity.id != null // must not be transient
&& !em.contains(entity) // must not be managed now
&& em.find(Entity.class, entity.id) != null; // must not have been removed
}
The above assumes that em
is the EntityManager
, Entity
is the entity class and has a public id
field that is a @GeneratedValue
primary key. (It also assumes that the row with this ID hasn't been removed from the database table by an external process in the time after the entity was detached.)

- 37,270
- 24
- 156
- 208
-
-
-
1In my case the Entity I am trying to update (merge) is always "detached". I ended up wrapping the ".merge()" with a try cacth block, checking on the IllegalArgumentException, which comes up, if it's detached. – Pwnstar Mar 08 '18 at 14:21
-
1And additionally I don't think an IllegalArgumentException is the right type of Exception for a "object is detached from PersistenceContext" :-) – Pwnstar Mar 08 '18 at 14:22
-
-
your boolean logic is wrong. `em.find(Entity.class, entity.id) != null` returns TRUE if entity IS not detached – Enerccio Sep 23 '20 at 16:02
-
1@Enerccio The third clause is to find out whether the entity row *still exists in the database* - otherwise it could be a new row that hasn't been persisted. Please see the last sentence of the answer for a caveat about this. – Steve Chambers Sep 24 '20 at 08:42
-