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);
}
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();
}
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);
}
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);
}
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);
}
Aggregations