Answering my own question.
I do not think this is possible with public APIs. However, if you are willing to endure some cheeses, you can do the following
SessionImplementor sessionImplementor = ((SessionImplementor)session); EntityPersister entityPersister = sessionImplementor.getFactory().getEntityPersister( clazz.getName() ); PersistenceContext persistenceContext = sessionImplementor.getPersistenceContext(); EntityKey entityKey = new EntityKey( id, entityPersister, EntityMode.POJO ); Object entity = persistenceContext.getEntity( entityKey ); if ( entity != null ) return entity; entity = persistenceContext.getProxy( entityKey ); if ( entity != null ) return entity; return null;
This depends on the internal hibernation APIs, so it may not work in the future if it changes internally.
source share