Example usage for org.springframework.orm.jpa ExtendedEntityManagerCreator createApplicationManagedEntityManager

List of usage examples for org.springframework.orm.jpa ExtendedEntityManagerCreator createApplicationManagedEntityManager

Introduction

In this page you can find the example usage for org.springframework.orm.jpa ExtendedEntityManagerCreator createApplicationManagedEntityManager.

Prototype

public static EntityManager createApplicationManagedEntityManager(EntityManager rawEntityManager,
        EntityManagerFactoryInfo emfInfo, boolean synchronizedWithTransaction) 

Source Link

Document

Create an application-managed extended EntityManager proxy.

Usage

From source file:org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.java

/**
 * Delegate an incoming invocation from the proxy, dispatching to EntityManagerFactoryInfo
 * or the native EntityManagerFactory accordingly.
 *///from w w  w .j  a  v  a  2s. c o  m
Object invokeProxyMethod(Method method, @Nullable Object[] args) throws Throwable {
    if (method.getDeclaringClass().isAssignableFrom(EntityManagerFactoryInfo.class)) {
        return method.invoke(this, args);
    } else if (method.getName().equals("createEntityManager") && args != null && args.length > 0
            && args[0] == SynchronizationType.SYNCHRONIZED) {
        // JPA 2.1's createEntityManager(SynchronizationType, Map)
        // Redirect to plain createEntityManager and add synchronization semantics through Spring proxy
        EntityManager rawEntityManager = (args.length > 1
                ? getNativeEntityManagerFactory().createEntityManager((Map<?, ?>) args[1])
                : getNativeEntityManagerFactory().createEntityManager());
        return ExtendedEntityManagerCreator.createApplicationManagedEntityManager(rawEntityManager, this, true);
    }

    // Look for Query arguments, primarily JPA 2.1's addNamedQuery(String, Query)
    if (args != null) {
        for (int i = 0; i < args.length; i++) {
            Object arg = args[i];
            if (arg instanceof Query && Proxy.isProxyClass(arg.getClass())) {
                // Assumably a Spring-generated proxy from SharedEntityManagerCreator:
                // since we're passing it back to the native EntityManagerFactory,
                // let's unwrap it to the original Query object from the provider.
                try {
                    args[i] = ((Query) arg).unwrap(null);
                } catch (RuntimeException ex) {
                    // Ignore - simply proceed with given Query object then
                }
            }
        }
    }

    // Standard delegation to the native factory, just post-processing EntityManager return values
    Object retVal = method.invoke(getNativeEntityManagerFactory(), args);
    if (retVal instanceof EntityManager) {
        // Any other createEntityManager variant - expecting non-synchronized semantics
        EntityManager rawEntityManager = (EntityManager) retVal;
        retVal = ExtendedEntityManagerCreator.createApplicationManagedEntityManager(rawEntityManager, this,
                false);
    }
    return retVal;
}