use of jakarta.persistence.Query 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.Query in project spring-framework by spring-projects.
the class AbstractContainerEntityManagerFactoryIntegrationTests method testMultipleResults.
@Test
@SuppressWarnings("unchecked")
public void testMultipleResults() {
// Add with JDBC
String firstName = "Tony";
insertPerson(firstName);
assertThat(Proxy.isProxyClass(sharedEntityManager.getClass())).isTrue();
Query q = sharedEntityManager.createQuery("select p from Person as p");
List<Person> people = q.getResultList();
assertThat(people.size()).isEqualTo(1);
assertThat(people.get(0).getFirstName()).isEqualTo(firstName);
}
use of jakarta.persistence.Query 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);
}
use of jakarta.persistence.Query in project spring-framework by spring-projects.
the class AbstractContainerEntityManagerFactoryIntegrationTests method testEntityManagerProxyIsProxy.
@Test
public void testEntityManagerProxyIsProxy() {
assertThat(Proxy.isProxyClass(sharedEntityManager.getClass())).isTrue();
Query q = sharedEntityManager.createQuery("select p from Person as p");
q.getResultList();
assertThat(sharedEntityManager.isOpen()).as("Should be open to start with").isTrue();
sharedEntityManager.close();
assertThat(sharedEntityManager.isOpen()).as("Close should have been silently ignored").isTrue();
}
use of jakarta.persistence.Query 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();
}
Aggregations