Search in sources :

Example 21 with EntityManager

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

the class DefaultJpaDialectTests method testDefaultBeginTransaction.

@Test
public void testDefaultBeginTransaction() throws Exception {
    TransactionDefinition definition = new DefaultTransactionDefinition();
    EntityManager entityManager = mock(EntityManager.class);
    EntityTransaction entityTx = mock(EntityTransaction.class);
    given(entityManager.getTransaction()).willReturn(entityTx);
    dialect.beginTransaction(entityManager, definition);
}
Also used : TransactionDefinition(org.springframework.transaction.TransactionDefinition) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) EntityTransaction(jakarta.persistence.EntityTransaction) EntityManager(jakarta.persistence.EntityManager) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Test(org.junit.jupiter.api.Test)

Example 22 with EntityManager

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

the class LocalContainerEntityManagerFactoryBeanTests method testApplicationManagedEntityManagerWithJtaTransaction.

@Test
public void testApplicationManagedEntityManagerWithJtaTransaction() throws Exception {
    Object testEntity = new Object();
    // This one's for the tx (shared)
    EntityManager sharedEm = mock(EntityManager.class);
    given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());
    // This is the application-specific one
    EntityManager mockEm = mock(EntityManager.class);
    given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);
    LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
    MutablePersistenceUnitInfo pui = ((MutablePersistenceUnitInfo) cefb.getPersistenceUnitInfo());
    pui.setTransactionType(PersistenceUnitTransactionType.JTA);
    JpaTransactionManager jpatm = new JpaTransactionManager();
    jpatm.setEntityManagerFactory(cefb.getObject());
    TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());
    EntityManagerFactory emf = cefb.getObject();
    assertThat(cefb.getObject()).as("EntityManagerFactory reference must be cached after init").isSameAs(emf);
    assertThat(emf).as("EMF must be proxied").isNotSameAs(mockEmf);
    EntityManager em = emf.createEntityManager();
    em.joinTransaction();
    assertThat(em.contains(testEntity)).isFalse();
    jpatm.commit(txStatus);
    cefb.destroy();
    verify(mockEm).joinTransaction();
    verify(mockEm).contains(testEntity);
    verify(mockEmf).close();
}
Also used : EntityManager(jakarta.persistence.EntityManager) DefaultTransactionAttribute(org.springframework.transaction.interceptor.DefaultTransactionAttribute) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) TransactionStatus(org.springframework.transaction.TransactionStatus) MutablePersistenceUnitInfo(org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo) Test(org.junit.jupiter.api.Test)

Example 23 with EntityManager

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

the class AbstractContainerEntityManagerFactoryIntegrationTests method testQueryNoPersons.

@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersons() {
    EntityManager em = entityManagerFactory.createEntityManager();
    Query q = em.createQuery("select p from Person as p");
    List<Person> people = q.getResultList();
    assertThat(people.size()).isEqualTo(0);
    assertThatExceptionOfType(NoResultException.class).isThrownBy(q::getSingleResult);
}
Also used : EntityManager(jakarta.persistence.EntityManager) Query(jakarta.persistence.Query) NoResultException(jakarta.persistence.NoResultException) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.jupiter.api.Test)

Example 24 with EntityManager

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

the class AbstractContainerEntityManagerFactoryIntegrationTests method testQueryNoPersonsSharedNotTransactional.

@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersonsSharedNotTransactional() {
    endTransaction();
    EntityManager em = this.sharedEntityManager;
    Query q = em.createQuery("select p from Person as p");
    q.setFlushMode(FlushModeType.AUTO);
    List<Person> people = q.getResultList();
    assertThat(people.size()).isEqualTo(0);
    assertThatExceptionOfType(Exception.class).isThrownBy(q::getSingleResult).withMessageContaining("closed");
    // We would typically expect an IllegalStateException, but Hibernate throws a
    // PersistenceException. So we assert the contents of the exception message instead.
    Query q2 = em.createQuery("select p from Person as p");
    q2.setFlushMode(FlushModeType.AUTO);
    assertThatExceptionOfType(NoResultException.class).isThrownBy(q2::getSingleResult);
}
Also used : EntityManager(jakarta.persistence.EntityManager) Query(jakarta.persistence.Query) NoResultException(jakarta.persistence.NoResultException) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.jupiter.api.Test)

Example 25 with EntityManager

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

the class AbstractContainerEntityManagerFactoryIntegrationTests method testQueryNoPersonsNotTransactional.

@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersonsNotTransactional() {
    endTransaction();
    EntityManager em = entityManagerFactory.createEntityManager();
    Query q = em.createQuery("select p from Person as p");
    List<Person> people = q.getResultList();
    assertThat(people.size()).isEqualTo(0);
    assertThatExceptionOfType(NoResultException.class).isThrownBy(q::getSingleResult);
}
Also used : EntityManager(jakarta.persistence.EntityManager) Query(jakarta.persistence.Query) NoResultException(jakarta.persistence.NoResultException) Person(org.springframework.orm.jpa.domain.Person) 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