Search in sources :

Example 1 with Query

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

the class SharedEntityManagerCreatorTests method deferredQueryWithUpdate.

@Test
public void deferredQueryWithUpdate() {
    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").executeUpdate();
    verify(query).executeUpdate();
    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)

Example 2 with Query

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

Example 3 with Query

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

Example 4 with Query

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

the class AbstractContainerEntityManagerFactoryIntegrationTests method testQueryNoPersonsShared.

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

Example 5 with Query

use of jakarta.persistence.Query 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)

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