Get EntityManager containing an entity

Hello folks,

I’m migrating to Hibernate from OpenJPA, and wondering if Hibernate provides this feature I have become accustomed to.

It is a static method that finds the EntityManager that owns an entity (if any).
public static OpenJPAEntityManager getEntityManager(Object possibleEntity) …

I know it’s bad form, but this method is a convenience. Does something like that exist in Hibernate?

Thanks

If you use enhancement you could in theory cast the entity to org.hibernate.engine.spi.ManagedEntity. Access the entity entry through $$_hibernate_getEntityEntry() and cast that to org.hibernate.engine.internal.AbstractEntityEntry. Through that you can obtain the getPersistenceContext() which is connected to the session/entity manager.

That does the trick. Thank you!