Hibernate Lazy Loading Proxy Incompatible with Other Frameworks
I've come across several instances where frameworks that take POJOs to do some work crap-out with proxied hibernate beans.
For example if I xml annotate a bean for framework X and pass it to framework X it doesn't recognise the bean because it is passed the proxied object - which has no annotations for framework X.
Is there a common solution to 开发者_Python百科this? I'd prefer not to define the bean as eager loaded, or turn of lazy-loading anywhere in the application.
You can unproxy the object before passing it around:
public static <T> T initializeAndUnproxy(T var) {
if (var == null) {
throw new IllegalArgumentException("passed argument is null");
}
Hibernate.initialize(var);
if (var instanceof HibernateProxy) {
var = (T) ((HibernateProxy) var).getHibernateLazyInitializer()
.getImplementation();
}
return var;
}
精彩评论