use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class SharedEntityManagerCreatorTests method deferredStoredProcedureQueryWithIndexedParameters.
@Test
public void deferredStoredProcedureQueryWithIndexedParameters() {
EntityManagerFactory emf = mock(EntityManagerFactory.class);
EntityManager targetEm = mock(EntityManager.class);
StoredProcedureQuery query = mock(StoredProcedureQuery.class);
given(emf.createEntityManager()).willReturn(targetEm);
given(targetEm.createStoredProcedureQuery("x")).willReturn(query);
willReturn("y").given(query).getOutputParameterValue(0);
willReturn("z").given(query).getOutputParameterValue(2);
given(targetEm.isOpen()).willReturn(true);
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
StoredProcedureQuery spq = em.createStoredProcedureQuery("x");
spq.registerStoredProcedureParameter(0, String.class, ParameterMode.OUT);
spq.registerStoredProcedureParameter(1, Number.class, ParameterMode.IN);
spq.registerStoredProcedureParameter(2, Object.class, ParameterMode.INOUT);
spq.execute();
assertThat(spq.getOutputParameterValue(0)).isEqualTo("y");
assertThatIllegalArgumentException().isThrownBy(() -> spq.getOutputParameterValue(1));
assertThat(spq.getOutputParameterValue(2)).isEqualTo("z");
verify(query).registerStoredProcedureParameter(0, String.class, ParameterMode.OUT);
verify(query).registerStoredProcedureParameter(1, Number.class, ParameterMode.IN);
verify(query).registerStoredProcedureParameter(2, Object.class, ParameterMode.INOUT);
verify(query).execute();
verify(targetEm).close();
verifyNoMoreInteractions(query);
verifyNoMoreInteractions(targetEm);
}
use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class SharedEntityManagerCreatorTests method deferredQueryWithSingleResult.
@Test
public void deferredQueryWithSingleResult() {
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").getSingleResult();
verify(query).getSingleResult();
verify(targetEm).close();
}
use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class ApplicationManagedEntityManagerIntegrationTests method testEntityManagerProxyIsProxy.
@Test
@SuppressWarnings("unchecked")
public void testEntityManagerProxyIsProxy() {
EntityManager em = entityManagerFactory.createEntityManager();
assertThat(Proxy.isProxyClass(em.getClass())).isTrue();
Query q = em.createQuery("select p from Person as p");
List<Person> people = q.getResultList();
assertThat(people).isNotNull();
assertThat(em.isOpen()).as("Should be open to start with").isTrue();
em.close();
assertThat(em.isOpen()).as("Close should work on application managed EM").isFalse();
}
use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class ApplicationManagedEntityManagerIntegrationTests method testCannotFlushWithoutGettingTransaction.
@Test
public void testCannotFlushWithoutGettingTransaction() {
EntityManager em = entityManagerFactory.createEntityManager();
assertThatExceptionOfType(TransactionRequiredException.class).isThrownBy(() -> doInstantiateAndSave(em));
// TODO following lines are a workaround for Hibernate bug
// If Hibernate throws an exception due to flush(),
// it actually HAS flushed, meaning that the database
// was updated outside the transaction
deleteAllPeopleUsingEntityManager(sharedEntityManager);
setComplete();
}
use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class ApplicationManagedEntityManagerIntegrationTests method testInstantiateAndSave.
@Test
public void testInstantiateAndSave() {
EntityManager em = entityManagerFactory.createEntityManager();
em.joinTransaction();
doInstantiateAndSave(em);
}
Aggregations