views:

68

answers:

1

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 this? I'd prefer not to define the bean as eager loaded, or turn of lazy-loading anywhere in the application.

Thoughts? Thanks.

+2  A: 

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;
}
Bozho
Thanks. I guess the best solution is to slot this code into an interceptor before any framework gets it.
bowsie
Reused, so mandatory +1
Pascal Thivent