org.springframework.orm.jpa.EntityManagerProxy Java Examples

The following examples show how to use org.springframework.orm.jpa.EntityManagerProxy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PersistenceAnnotationBeanPostProcessor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private EntityManager resolveExtendedEntityManager(Object target, @Nullable 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 && requestingBeanName != null &&
			beanFactory.containsBean(requestingBeanName) && !beanFactory.isPrototype(requestingBeanName)) {
		extendedEntityManagersToClose.put(target, ((EntityManagerProxy) em).getTargetEntityManager());
	}
	return em;
}
 
Example #2
Source File: PersistenceAnnotationBeanPostProcessor.java    From java-technology-stack with MIT License 6 votes vote down vote up
private EntityManager resolveExtendedEntityManager(Object target, @Nullable 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 && requestingBeanName != null &&
			beanFactory.containsBean(requestingBeanName) && !beanFactory.isPrototype(requestingBeanName)) {
		extendedEntityManagersToClose.put(target, ((EntityManagerProxy) em).getTargetEntityManager());
	}
	return em;
}
 
Example #3
Source File: PersistenceAnnotationBeanPostProcessor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #4
Source File: PersistenceAnnotationBeanPostProcessor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #5
Source File: SharedEntityManagerFactoryTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@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();
}
 
Example #6
Source File: HibernateEntityManagerFactoryIntegrationTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testCanCastSharedEntityManagerProxyToHibernateEntityManager() {
	assertTrue(sharedEntityManager instanceof org.hibernate.jpa.HibernateEntityManager);
	assertTrue(((EntityManagerProxy) sharedEntityManager).getTargetEntityManager() instanceof Session);  // as of Hibernate 5.2
}
 
Example #7
Source File: SharedEntityManagerFactoryTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@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();
}
 
Example #8
Source File: HibernateEntityManagerFactoryIntegrationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testCanCastSharedEntityManagerProxyToHibernateEntityManager() {
	assertTrue(sharedEntityManager instanceof org.hibernate.jpa.HibernateEntityManager);
	assertTrue(((EntityManagerProxy) sharedEntityManager).getTargetEntityManager() instanceof Session);  // as of Hibernate 5.2
}
 
Example #9
Source File: SharedEntityManagerFactoryTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@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();
}