Created
March 12, 2013 10:24
-
-
Save thinkbigthings/5141813 to your computer and use it in GitHub Desktop.
How to get the entity behind a Hibernate proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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