Example usage for javax.persistence EntityManager isJoinedToTransaction

List of usage examples for javax.persistence EntityManager isJoinedToTransaction

Introduction

In this page you can find the example usage for javax.persistence EntityManager isJoinedToTransaction.

Prototype

public boolean isJoinedToTransaction();

Source Link

Document

Determine whether the entity manager is joined to the current transaction.

Usage

From source file:com.github.fharms.camel.entitymanager.CamelEntityManagerHandler.java

private <T> T createEntityManagerProxy(Class<T> interfaceClass, Object emProxy) {
    InvocationHandler handler = (proxy, method, args) -> {

        EntityManager em = entityManagerLocal.get() != null ? entityManagerLocal.get()
                : (EntityManager) emProxy;
        switch (method.getName()) {
        case "hashCode":
            return hashCode();
        case "equals":
            return (em == args[0]);
        case "toString":
            return "Camel EntityManager proxy [" + em.toString() + "]";
        }/*from  w  ww .  j  a  v  a 2s  .  co m*/

        if (!em.isJoinedToTransaction()) {
            em.joinTransaction();
        }
        return method.invoke(em, args);
    };
    return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[] { interfaceClass }, handler);
}