use of jakarta.persistence.EntityManagerFactory in project spring-framework by spring-projects.
the class PersistenceInjectionTests method testPublicPersistenceUnitSetterWithOverriding.
@Test
public void testPublicPersistenceUnitSetterWithOverriding() {
EntityManagerFactory mockEmf2 = mock(EntityManagerFactory.class);
GenericApplicationContext gac = new GenericApplicationContext();
gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf);
gac.registerBeanDefinition("annotationProcessor", new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class));
RootBeanDefinition bd = new RootBeanDefinition(DefaultPublicPersistenceUnitSetter.class);
bd.getPropertyValues().add("emf", mockEmf2);
gac.registerBeanDefinition(DefaultPublicPersistenceUnitSetter.class.getName(), bd);
gac.refresh();
DefaultPublicPersistenceUnitSetter bean = (DefaultPublicPersistenceUnitSetter) gac.getBean(DefaultPublicPersistenceUnitSetter.class.getName());
assertThat(bean.emf).isSameAs(mockEmf2);
}
use of jakarta.persistence.EntityManagerFactory in project spring-framework by spring-projects.
the class PersistenceInjectionTests method testPublicPersistenceUnitSetterWithUnitIdentifiedThroughBeanName.
@Test
public void testPublicPersistenceUnitSetterWithUnitIdentifiedThroughBeanName() {
EntityManagerFactory mockEmf2 = mock(EntityManagerFactory.class);
GenericApplicationContext gac = new GenericApplicationContext();
gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf);
gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory2", mockEmf2);
gac.registerAlias("entityManagerFactory2", "Person");
RootBeanDefinition processorDef = new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class);
processorDef.getPropertyValues().add("defaultPersistenceUnitName", "entityManagerFactory");
gac.registerBeanDefinition("annotationProcessor", processorDef);
gac.registerBeanDefinition(DefaultPublicPersistenceUnitSetter.class.getName(), new RootBeanDefinition(DefaultPublicPersistenceUnitSetter.class));
gac.registerBeanDefinition(DefaultPublicPersistenceUnitSetterNamedPerson.class.getName(), new RootBeanDefinition(DefaultPublicPersistenceUnitSetterNamedPerson.class));
gac.refresh();
DefaultPublicPersistenceUnitSetter bean = (DefaultPublicPersistenceUnitSetter) gac.getBean(DefaultPublicPersistenceUnitSetter.class.getName());
DefaultPublicPersistenceUnitSetterNamedPerson bean2 = (DefaultPublicPersistenceUnitSetterNamedPerson) gac.getBean(DefaultPublicPersistenceUnitSetterNamedPerson.class.getName());
assertThat(bean.emf).isSameAs(mockEmf);
assertThat(bean2.emf).isSameAs(mockEmf2);
}
use of jakarta.persistence.EntityManagerFactory in project spring-framework by spring-projects.
the class SharedEntityManagerCreatorTests method deferredQueryWithResultList.
@Test
public void deferredQueryWithResultList() {
EntityManagerFactory emf = mock(EntityManagerFactory.class);
EntityManager targetEm = mock(EntityManager.class);
Query query = mock(Query.class);
given(emf.createEntityManager()).willReturn(targetEm);
given(targetEm.createQuery("x")).willReturn(query);
given(targetEm.isOpen()).willReturn(true);
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
em.createQuery("x").getResultList();
verify(query).getResultList();
verify(targetEm).close();
}
use of jakarta.persistence.EntityManagerFactory in project spring-framework by spring-projects.
the class EntityManagerFactoryUtilsTests method testDoGetEntityManagerWithTx.
@Test
public void testDoGetEntityManagerWithTx() throws Exception {
try {
EntityManagerFactory factory = mock(EntityManagerFactory.class);
EntityManager manager = mock(EntityManager.class);
TransactionSynchronizationManager.initSynchronization();
given(factory.createEntityManager()).willReturn(manager);
// no tx active
assertThat(EntityManagerFactoryUtils.doGetTransactionalEntityManager(factory, null)).isSameAs(manager);
assertThat(((EntityManagerHolder) TransactionSynchronizationManager.unbindResource(factory)).getEntityManager()).isSameAs(manager);
} finally {
TransactionSynchronizationManager.clearSynchronization();
}
assertThat(TransactionSynchronizationManager.getResourceMap().isEmpty()).isTrue();
}
use of jakarta.persistence.EntityManagerFactory in project spring-framework by spring-projects.
the class EntityManagerFactoryUtilsTests method testDoGetEntityManager.
/*
* Test method for
* 'org.springframework.orm.jpa.EntityManagerFactoryUtils.doGetEntityManager(EntityManagerFactory)'
*/
@Test
public void testDoGetEntityManager() {
// test null assertion
assertThatIllegalArgumentException().isThrownBy(() -> EntityManagerFactoryUtils.doGetTransactionalEntityManager(null, null));
EntityManagerFactory factory = mock(EntityManagerFactory.class);
// no tx active
assertThat(EntityManagerFactoryUtils.doGetTransactionalEntityManager(factory, null)).isNull();
assertThat(TransactionSynchronizationManager.getResourceMap().isEmpty()).isTrue();
}
Aggregations