use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class SharedEntityManagerCreatorTests method transactionRequiredExceptionOnFlush.
@Test
public void transactionRequiredExceptionOnFlush() {
EntityManagerFactory emf = mock(EntityManagerFactory.class);
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
assertThatExceptionOfType(TransactionRequiredException.class).isThrownBy(em::flush);
}
use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class PersistenceInjectionTests method testPropertiesForSharedEntityManager1.
/**
* Binds an EMF to the thread and tests if EM with different properties
* generate new EMs or not.
*/
@Test
public void testPropertiesForSharedEntityManager1() {
Properties props = new Properties();
props.put("foo", "bar");
EntityManager em = mock(EntityManager.class);
// only one call made - the first EM definition wins (in this case the one w/ the properties)
given(mockEmf.createEntityManager(props)).willReturn(em);
given(em.getDelegate()).willReturn(new Object());
given(em.isOpen()).willReturn(true);
PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor();
DefaultPrivatePersistenceContextFieldWithProperties transactionalFieldWithProperties = new DefaultPrivatePersistenceContextFieldWithProperties();
DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField();
pabpp.postProcessProperties(null, transactionalFieldWithProperties, "bean1");
pabpp.postProcessProperties(null, transactionalField, "bean2");
assertThat(transactionalFieldWithProperties.em).isNotNull();
assertThat(transactionalField.em).isNotNull();
// the EM w/ properties will be created
assertThat(transactionalFieldWithProperties.em.getDelegate()).isNotNull();
// bind em to the thread now since it's created
try {
TransactionSynchronizationManager.bindResource(mockEmf, new EntityManagerHolder(em));
assertThat(transactionalField.em.getDelegate()).isNotNull();
verify(em).close();
} finally {
TransactionSynchronizationManager.unbindResource(mockEmf);
}
}
use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class PersistenceInjectionTests method testPropertiesForSharedEntityManager2.
@Test
public void testPropertiesForSharedEntityManager2() {
Properties props = new Properties();
props.put("foo", "bar");
EntityManager em = mock(EntityManager.class);
// only one call made - the first EM definition wins (in this case the one w/o the properties)
given(mockEmf.createEntityManager()).willReturn(em);
given(em.getDelegate()).willReturn(new Object(), 2);
given(em.isOpen()).willReturn(true);
PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor();
DefaultPrivatePersistenceContextFieldWithProperties transactionalFieldWithProperties = new DefaultPrivatePersistenceContextFieldWithProperties();
DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField();
pabpp.postProcessProperties(null, transactionalFieldWithProperties, "bean1");
pabpp.postProcessProperties(null, transactionalField, "bean2");
assertThat(transactionalFieldWithProperties.em).isNotNull();
assertThat(transactionalField.em).isNotNull();
// the EM w/o properties will be created
assertThat(transactionalField.em.getDelegate()).isNotNull();
// bind em to the thread now since it's created
try {
TransactionSynchronizationManager.bindResource(mockEmf, new EntityManagerHolder(em));
assertThat(transactionalFieldWithProperties.em.getDelegate()).isNotNull();
verify(em).close();
} finally {
TransactionSynchronizationManager.unbindResource(mockEmf);
}
}
use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class PersistenceInjectionTests method testPublicSpecificExtendedPersistenceContextSetter.
@Test
public void testPublicSpecificExtendedPersistenceContextSetter() {
EntityManagerFactory mockEmf2 = mock(EntityManagerFactory.class);
EntityManager mockEm2 = mock(EntityManager.class);
given(mockEmf2.createEntityManager()).willReturn(mockEm2);
GenericApplicationContext gac = new GenericApplicationContext();
gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf);
gac.getDefaultListableBeanFactory().registerSingleton("unit2", mockEmf2);
gac.registerBeanDefinition("annotationProcessor", new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class));
gac.registerBeanDefinition(SpecificPublicPersistenceContextSetter.class.getName(), new RootBeanDefinition(SpecificPublicPersistenceContextSetter.class));
gac.refresh();
SpecificPublicPersistenceContextSetter bean = (SpecificPublicPersistenceContextSetter) gac.getBean(SpecificPublicPersistenceContextSetter.class.getName());
assertThat(bean.getEntityManager()).isNotNull();
bean.getEntityManager().flush();
verify(mockEm2).getTransaction();
verify(mockEm2).flush();
}
use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class SharedEntityManagerFactoryTests method testValidUsage.
@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();
assertThat(EntityManager.class.isAssignableFrom(proxyFactoryBean.getObjectType())).isTrue();
assertThat(proxyFactoryBean.isSingleton()).isTrue();
EntityManager proxy = proxyFactoryBean.getObject();
assertThat(proxyFactoryBean.getObject()).isSameAs(proxy);
assertThat(proxy.contains(o)).isFalse();
boolean condition = proxy instanceof EntityManagerProxy;
assertThat(condition).isTrue();
EntityManagerProxy emProxy = (EntityManagerProxy) proxy;
assertThatIllegalStateException().as("outside of transaction").isThrownBy(emProxy::getTargetEntityManager);
TransactionSynchronizationManager.bindResource(mockEmf, new EntityManagerHolder(mockEm));
try {
assertThat(emProxy.getTargetEntityManager()).isSameAs(mockEm);
} finally {
TransactionSynchronizationManager.unbindResource(mockEmf);
}
assertThat(TransactionSynchronizationManager.getResourceMap().isEmpty()).isTrue();
verify(mockEm).contains(o);
verify(mockEm).close();
}
Aggregations