Search in sources :

Example 6 with EntityManager

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

Example 7 with EntityManager

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);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Properties(java.util.Properties) EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder) Test(org.junit.jupiter.api.Test)

Example 8 with EntityManager

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);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Properties(java.util.Properties) EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder) Test(org.junit.jupiter.api.Test)

Example 9 with EntityManager

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

Example 10 with EntityManager

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

Aggregations

EntityManager (jakarta.persistence.EntityManager)55 Test (org.junit.jupiter.api.Test)52 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)25 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