Hibernate Fetch profiles not working in JPA 2.0

My use case needs to load collections which are lazy.

So, I am implementing some fetchprofiles that suit my needs.

@FetchProfiles(value= {
    @FetchProfile(name=AppUser.EAGER_PROP1_PROP2_PROP3, fetchOverrides= {
            @FetchProfile.FetchOverride(entity=MyEntity.class, association="property1", mode=FetchMode.JOIN),
            @FetchProfile.FetchOverride(entity=MyEntity.class, association="property2", mode=FetchMode.JOIN),
            @FetchProfile.FetchOverride(entity=MyEntity.class, association="property3", mode=FetchMode.JOIN)
    }),
    @FetchProfile(name=AppUser.EAGER_PROP1_PROP2, fetchOverrides= {
            @FetchProfile.FetchOverride(entity=MyEntity.class, association="property1", mode=FetchMode.JOIN),
            @FetchProfile.FetchOverride(entity=MyEntity.class, association="property2", mode=FetchMode.JOIN)
    })
})

      

To enable fetch profiles, I wrote an interceptor. This interceptor activates and deactivates the given sampling profile

import java.lang.reflect.Method;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import javax.persistence.EntityManager;
import org.hibernate.Session;

@Interceptor
public class FetchProfileInterceptor {

    @AroundInvoke
    public Object intercept(InvocationContext ctx) throws Exception {
        Session session = null;
        try {
            session = retrieveSession(ctx);
        } catch (Exception e) {
            throw e;
        }

        Object returnValue;

        if (ctx.getParameters().length > 1) {
            String[] fetchProfiles = (String[]) ctx.getParameters()[1];
            for(String profile : fetchProfiles) {
                session.enableFetchProfile(profile);
            }

            returnValue = ctx.proceed();

            for(String profile : fetchProfiles) {
                session.disableFetchProfile(profile);
            }
        } else {
            throw new Exception("This method has the wrong signature! Need is \"SomeObject obj, String ... fetchProfile\" signature!");
        }
        return returnValue;
    }

    private Session retrieveSession(InvocationContext ctx) throws Exception {
        try {
            Object target = ctx.getTarget();
            Method method = target.getClass().getMethod("getEntityManager");
            EntityManager manager = (EntityManager) method.invoke(target);
            return manager.unwrap(Session.class);
        } catch (Exception e) {
            throw new Exception("Classes that invoke FetchProfileInterceptor must implement the method \"getEntityManager\" with the return type " + EntityManager.class.getCanonicalName());
        }
    }
}

      

And my service method in EJB:

@Interceptors(FetchProfileInterceptor.class)
public AppUser findByUsername(final String username, String ... overwriteFetchProfile) {
    return repository.findByUsername(username);
}

      

When I debug the code the interceptor is called. Fetchprofile is installed but has no effect. Did I forget something or did something wrong?

+3


source to share





All Articles