Java 类org.springframework.orm.jpa.EntityManagerProxy 实例源码

项目:lams    文件:PersistenceAnnotationBeanPostProcessor.java   
private EntityManager resolveExtendedEntityManager(Object target, String requestingBeanName) {
    // Obtain EntityManager reference from JNDI?
    EntityManager em = getPersistenceContext(this.unitName, true);
    if (em == null) {
        // No pre-built EntityManager found -> build one based on factory.
        // Obtain EntityManagerFactory from JNDI?
        EntityManagerFactory emf = getPersistenceUnit(this.unitName);
        if (emf == null) {
            // Need to search for EntityManagerFactory beans.
            emf = findEntityManagerFactory(this.unitName, requestingBeanName);
        }
        // Inject a container-managed extended EntityManager.
        em = ExtendedEntityManagerCreator.createContainerManagedEntityManager(
                emf, this.properties, this.synchronizedWithTransaction);
    }
    if (em instanceof EntityManagerProxy &&
            beanFactory != null && !beanFactory.isPrototype(requestingBeanName)) {
        extendedEntityManagersToClose.put(target, ((EntityManagerProxy) em).getTargetEntityManager());
    }
    return em;
}
项目:spring4-understanding    文件:PersistenceAnnotationBeanPostProcessor.java   
private EntityManager resolveExtendedEntityManager(Object target, String requestingBeanName) {
    // Obtain EntityManager reference from JNDI?
    EntityManager em = getPersistenceContext(this.unitName, true);
    if (em == null) {
        // No pre-built EntityManager found -> build one based on factory.
        // Obtain EntityManagerFactory from JNDI?
        EntityManagerFactory emf = getPersistenceUnit(this.unitName);
        if (emf == null) {
            // Need to search for EntityManagerFactory beans.
            emf = findEntityManagerFactory(this.unitName, requestingBeanName);
        }
        // Inject a container-managed extended EntityManager.
        em = ExtendedEntityManagerCreator.createContainerManagedEntityManager(
                emf, this.properties, this.synchronizedWithTransaction);
    }
    if (em instanceof EntityManagerProxy && beanFactory != null &&
            beanFactory.containsBean(requestingBeanName) && !beanFactory.isPrototype(requestingBeanName)) {
        extendedEntityManagersToClose.put(target, ((EntityManagerProxy) em).getTargetEntityManager());
    }
    return em;
}
项目:class-guard    文件:PersistenceAnnotationBeanPostProcessor.java   
private EntityManager resolveExtendedEntityManager(Object target, String requestingBeanName) {
    // Obtain EntityManager reference from JNDI?
    EntityManager em = getPersistenceContext(this.unitName, true);
    if (em == null) {
        // No pre-built EntityManager found -> build one based on factory.
        // Obtain EntityManagerFactory from JNDI?
        EntityManagerFactory emf = getPersistenceUnit(this.unitName);
        if (emf == null) {
            // Need to search for EntityManagerFactory beans.
            emf = findEntityManagerFactory(this.unitName, requestingBeanName);
        }
        // Inject a container-managed extended EntityManager.
        em = ExtendedEntityManagerCreator.createContainerManagedEntityManager(emf, this.properties);
    }
    if (em instanceof EntityManagerProxy &&
            beanFactory != null && !beanFactory.isPrototype(requestingBeanName)) {
        extendedEntityManagersToClose.put(target, ((EntityManagerProxy) em).getTargetEntityManager());
    }
    return em;
}
项目:amanda    文件:ArticleRepositoryImpl.java   
@PostConstruct
public void initialize() {
    if(!(this.entityManager instanceof EntityManagerProxy))
        throw new FatalBeanException("Entity manager " + this.entityManager + " was not a proxy");

    this.entityManagerProxy = (EntityManagerProxy) this.entityManager;
}
项目:bookManager    文件:BookDatabaseImpl.java   
@PostConstruct
public void init() {
    if (!(this.entityManager instanceof EntityManagerProxy)) {
        throw new FatalBeanException("Entity Manager" + this.entityManager + " is not a proxy");
    }
    this.proxy = (EntityManagerProxy) this.entityManager;
}
项目:LSChatServer    文件:UserDatabaseImpl.java   
@PostConstruct
public void init() {
    if (!(this.entityManager instanceof EntityManagerProxy)) {
        throw new FatalBeanException("Entity Manager" + this.entityManager + " is not a proxy");
    }
    this.proxy = (EntityManagerProxy) this.entityManager;
}
项目:spring4-understanding    文件:SharedEntityManagerFactoryTests.java   
@Test
public void testValidUsage() {
    Object o = new Object();

    EntityManager mockEm = mock(EntityManager.class);
    given(mockEm.isOpen()).willReturn(true);

    EntityManagerFactory mockEmf = mock(EntityManagerFactory.class);
    given(mockEmf.createEntityManager()).willReturn(mockEm);

    SharedEntityManagerBean proxyFactoryBean = new SharedEntityManagerBean();
    proxyFactoryBean.setEntityManagerFactory(mockEmf);
    proxyFactoryBean.afterPropertiesSet();

    assertTrue(EntityManager.class.isAssignableFrom(proxyFactoryBean.getObjectType()));
    assertTrue(proxyFactoryBean.isSingleton());

    EntityManager proxy = proxyFactoryBean.getObject();
    assertSame(proxy, proxyFactoryBean.getObject());
    assertFalse(proxy.contains(o));

    assertTrue(proxy instanceof EntityManagerProxy);
    EntityManagerProxy emProxy = (EntityManagerProxy) proxy;
    try {
        emProxy.getTargetEntityManager();
        fail("Should have thrown IllegalStateException outside of transaction");
    }
    catch (IllegalStateException ex) {
        // expected
    }

    TransactionSynchronizationManager.bindResource(mockEmf, new EntityManagerHolder(mockEm));
    try {
        assertSame(mockEm, emProxy.getTargetEntityManager());
    }
    finally {
        TransactionSynchronizationManager.unbindResource(mockEmf);
    }

    assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
    verify(mockEm).contains(o);
    verify(mockEm).close();
}
项目:class-guard    文件:SharedEntityManagerFactoryTests.java   
@Test
public void testValidUsage() {
    Object o = new Object();

    EntityManager mockEm = mock(EntityManager.class);
    given(mockEm.isOpen()).willReturn(true);

    EntityManagerFactory mockEmf = mock(EntityManagerFactory.class);
    given(mockEmf.createEntityManager()).willReturn(mockEm);

    SharedEntityManagerBean proxyFactoryBean = new SharedEntityManagerBean();
    proxyFactoryBean.setEntityManagerFactory(mockEmf);
    proxyFactoryBean.afterPropertiesSet();

    assertTrue(EntityManager.class.isAssignableFrom(proxyFactoryBean.getObjectType()));
    assertTrue(proxyFactoryBean.isSingleton());

    EntityManager proxy = proxyFactoryBean.getObject();
    assertSame(proxy, proxyFactoryBean.getObject());
    assertFalse(proxy.contains(o));

    assertTrue(proxy instanceof EntityManagerProxy);
    EntityManagerProxy emProxy = (EntityManagerProxy) proxy;
    try {
        emProxy.getTargetEntityManager();
        fail("Should have thrown IllegalStateException outside of transaction");
    }
    catch (IllegalStateException ex) {
        // expected
    }

    TransactionSynchronizationManager.bindResource(mockEmf, new EntityManagerHolder(mockEm));
    try {
        assertSame(mockEm, emProxy.getTargetEntityManager());
    }
    finally {
        TransactionSynchronizationManager.unbindResource(mockEmf);
    }

    assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
    verify(mockEm).contains(o);
    verify(mockEm).close();
}