3

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.

1 Answer 1

6

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I guess the best solution is to slot this code into an interceptor before any framework gets it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.