Search in sources :

Example 6 with Query

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);
}
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 7 with Query

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);
}
Also used : Query(jakarta.persistence.Query) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.jupiter.api.Test)

Example 8 with Query

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);
}
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 9 with Query

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

Example 10 with Query

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

Aggregations

Query (jakarta.persistence.Query)13 Test (org.junit.jupiter.api.Test)13 EntityManager (jakarta.persistence.EntityManager)9 Person (org.springframework.orm.jpa.domain.Person)7 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)4 NoResultException (jakarta.persistence.NoResultException)4 StoredProcedureQuery (jakarta.persistence.StoredProcedureQuery)4