Search in sources :

Example 56 with EntityManager

use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.

the class PersistenceInjectionTests method testPersistenceContextsFromJndiWithDefaultUnit.

@Test
public void testPersistenceContextsFromJndiWithDefaultUnit() {
    EntityManager mockEm = mock(EntityManager.class);
    EntityManager mockEm2 = mock(EntityManager.class);
    EntityManager mockEm3 = mock(EntityManager.class);
    Map<String, String> persistenceContexts = new HashMap<>();
    persistenceContexts.put("System", "pc1");
    persistenceContexts.put("Person", "pc2");
    Map<String, String> extendedPersistenceContexts = new HashMap<>();
    extendedPersistenceContexts.put("System", "pc3");
    ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
    jt.addObject("java:comp/env/pc1", mockEm);
    jt.addObject("java:comp/env/pc2", mockEm2);
    jt.addObject("java:comp/env/pc3", mockEm3);
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
    bpp.setPersistenceContexts(persistenceContexts);
    bpp.setExtendedPersistenceContexts(extendedPersistenceContexts);
    bpp.setDefaultPersistenceUnitName("System");
    bpp.setJndiTemplate(jt);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition(DefaultPrivatePersistenceContextField.class.getName(), new RootBeanDefinition(DefaultPrivatePersistenceContextField.class));
    bf.registerBeanDefinition(DefaultPrivatePersistenceContextFieldNamedPerson.class.getName(), new RootBeanDefinition(DefaultPrivatePersistenceContextFieldNamedPerson.class));
    bf.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(), new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class));
    DefaultPrivatePersistenceContextField bean1 = (DefaultPrivatePersistenceContextField) bf.getBean(DefaultPrivatePersistenceContextField.class.getName());
    DefaultPrivatePersistenceContextFieldNamedPerson bean2 = (DefaultPrivatePersistenceContextFieldNamedPerson) bf.getBean(DefaultPrivatePersistenceContextFieldNamedPerson.class.getName());
    DefaultPublicPersistenceContextSetter bean3 = (DefaultPublicPersistenceContextSetter) bf.getBean(DefaultPublicPersistenceContextSetter.class.getName());
    assertThat(bean1.em).isSameAs(mockEm);
    assertThat(bean2.em).isSameAs(mockEm2);
    assertThat(bean3.em).isSameAs(mockEm3);
}
Also used : EntityManager(jakarta.persistence.EntityManager) HashMap(java.util.HashMap) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ExpectedLookupTemplate(org.springframework.context.testfixture.jndi.ExpectedLookupTemplate) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 57 with EntityManager

use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.

the class PersistenceInjectionTests method testSinglePersistenceContextFromJndi.

@Test
public void testSinglePersistenceContextFromJndi() {
    EntityManager mockEm = mock(EntityManager.class);
    EntityManager mockEm2 = mock(EntityManager.class);
    Map<String, String> persistenceContexts = new HashMap<>();
    persistenceContexts.put("System", "pc1");
    Map<String, String> extendedPersistenceContexts = new HashMap<>();
    extendedPersistenceContexts.put("System", "pc2");
    ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
    jt.addObject("java:comp/env/pc1", mockEm);
    jt.addObject("java:comp/env/pc2", mockEm2);
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
    bpp.setPersistenceContexts(persistenceContexts);
    bpp.setExtendedPersistenceContexts(extendedPersistenceContexts);
    bpp.setJndiTemplate(jt);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition(DefaultPrivatePersistenceContextField.class.getName(), new RootBeanDefinition(DefaultPrivatePersistenceContextField.class));
    bf.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(), new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class));
    DefaultPrivatePersistenceContextField bean1 = (DefaultPrivatePersistenceContextField) bf.getBean(DefaultPrivatePersistenceContextField.class.getName());
    DefaultPublicPersistenceContextSetter bean2 = (DefaultPublicPersistenceContextSetter) bf.getBean(DefaultPublicPersistenceContextSetter.class.getName());
    assertThat(bean1.em).isSameAs(mockEm);
    assertThat(bean2.em).isSameAs(mockEm2);
}
Also used : EntityManager(jakarta.persistence.EntityManager) HashMap(java.util.HashMap) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ExpectedLookupTemplate(org.springframework.context.testfixture.jndi.ExpectedLookupTemplate) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 58 with EntityManager

use of jakarta.persistence.EntityManager 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)

Aggregations

EntityManager (jakarta.persistence.EntityManager)58 Test (org.junit.jupiter.api.Test)53 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)26 Query (jakarta.persistence.Query)9 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)9 TransactionRequiredException (jakarta.persistence.TransactionRequiredException)7 Person (org.springframework.orm.jpa.domain.Person)7 StoredProcedureQuery (jakarta.persistence.StoredProcedureQuery)6 Properties (java.util.Properties)5 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)5 EntityManagerHolder (org.springframework.orm.jpa.EntityManagerHolder)5 EntityTransaction (jakarta.persistence.EntityTransaction)4 HashMap (java.util.HashMap)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 ExpectedLookupTemplate (org.springframework.context.testfixture.jndi.ExpectedLookupTemplate)4 TransactionStatus (org.springframework.transaction.TransactionStatus)4 NoResultException (jakarta.persistence.NoResultException)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 AfterEach (org.junit.jupiter.api.AfterEach)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3