Skip to content

Instantly share code, notes, and snippets.

@thinkbigthings
Created March 12, 2013 10:24
Show Gist options
  • Save thinkbigthings/5141813 to your computer and use it in GitHub Desktop.
Save thinkbigthings/5141813 to your computer and use it in GitHub Desktop.
How to get the entity behind a Hibernate proxy
import org.hibernate.Hibernate;
import org.hibernate.proxy.HibernateProxy;
public class HibernateUnproxifier<T> {
@SuppressWarnings("unchecked")
public T unproxy(T entity) {
if (entity == null) {
throw new NullPointerException("Entity passed for initialization is null");
}
Hibernate.initialize(entity);
if (entity instanceof HibernateProxy) {
entity = (T) ((HibernateProxy) entity).getHibernateLazyInitializer().getImplementation();
}
return entity;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment