Search in sources :

Example 21 with EntityManagerFactory

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);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 22 with EntityManagerFactory

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);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 23 with EntityManagerFactory

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();
}
Also used : EntityManager(jakarta.persistence.EntityManager) StoredProcedureQuery(jakarta.persistence.StoredProcedureQuery) Query(jakarta.persistence.Query) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) Test(org.junit.jupiter.api.Test)

Example 24 with EntityManagerFactory

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();
}
Also used : EntityManager(jakarta.persistence.EntityManager) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) Test(org.junit.jupiter.api.Test)

Example 25 with EntityManagerFactory

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();
}
Also used : EntityManagerFactory(jakarta.persistence.EntityManagerFactory) Test(org.junit.jupiter.api.Test)

Aggregations

EntityManagerFactory (jakarta.persistence.EntityManagerFactory)34 Test (org.junit.jupiter.api.Test)27 EntityManager (jakarta.persistence.EntityManager)25 StoredProcedureQuery (jakarta.persistence.StoredProcedureQuery)6 TransactionRequiredException (jakarta.persistence.TransactionRequiredException)6 Query (jakarta.persistence.Query)4 WebAsyncManager (org.springframework.web.context.request.async.WebAsyncManager)4 EntityTransaction (jakarta.persistence.EntityTransaction)3 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)3 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)3 EntityManagerHolder (org.springframework.orm.jpa.EntityManagerHolder)3 PersistenceException (jakarta.persistence.PersistenceException)2 AsyncEvent (jakarta.servlet.AsyncEvent)2 AsyncListener (jakarta.servlet.AsyncListener)2 FilterChain (jakarta.servlet.FilterChain)2 Callable (java.util.concurrent.Callable)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2