Search in sources :

Example 1 with Person

use of org.springframework.orm.jpa.domain.Person 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();
    assertEquals(0, people.size());
    try {
        assertNull(q.getSingleResult());
        fail("Should have thrown NoResultException");
    } catch (NoResultException ex) {
    // expected
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.Test)

Example 2 with Person

use of org.springframework.orm.jpa.domain.Person 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);
    assertTrue(Proxy.isProxyClass(sharedEntityManager.getClass()));
    Query q = sharedEntityManager.createQuery("select p from Person as p");
    List<Person> people = q.getResultList();
    assertEquals(1, people.size());
    assertEquals(firstName, people.get(0).getFirstName());
}
Also used : Query(javax.persistence.Query) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.Test)

Example 3 with Person

use of org.springframework.orm.jpa.domain.Person in project spring-framework by spring-projects.

the class AbstractContainerEntityManagerFactoryIntegrationTests method testQueryNoPersonsShared.

@Test
@SuppressWarnings({ "unused", "unchecked" })
public void testQueryNoPersonsShared() {
    EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(entityManagerFactory);
    Query q = em.createQuery("select p from Person as p");
    q.setFlushMode(FlushModeType.AUTO);
    List<Person> people = q.getResultList();
    try {
        assertNull(q.getSingleResult());
        fail("Should have thrown NoResultException");
    } catch (NoResultException ex) {
    // expected
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.Test)

Example 4 with Person

use of org.springframework.orm.jpa.domain.Person in project spring-framework by spring-projects.

the class ContainerManagedEntityManagerIntegrationTests method doInstantiateAndSave.

protected void doInstantiateAndSave(EntityManager em) {
    assertEquals("Should be no people from previous transactions", 0, countRowsInTable(em, "person"));
    Person p = new Person();
    p.setFirstName("Tony");
    p.setLastName("Blair");
    em.persist(p);
    em.flush();
    assertEquals("1 row must have been inserted", 1, countRowsInTable(em, "person"));
}
Also used : Person(org.springframework.orm.jpa.domain.Person)

Example 5 with Person

use of org.springframework.orm.jpa.domain.Person in project spring-framework by spring-projects.

the class AbstractContainerEntityManagerFactoryIntegrationTests method testQueryNoPersonsSharedNotTransactional.

@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersonsSharedNotTransactional() {
    endTransaction();
    EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(entityManagerFactory);
    Query q = em.createQuery("select p from Person as p");
    q.setFlushMode(FlushModeType.AUTO);
    List<Person> people = q.getResultList();
    assertEquals(0, people.size());
    try {
        assertNull(q.getSingleResult());
        fail("Should have thrown IllegalStateException");
    } catch (Exception ex) {
        // We would typically expect an IllegalStateException, but Hibernate throws a
        // PersistenceException. So we assert the contents of the exception message instead.
        assertTrue(ex.getMessage().contains("closed"));
    }
    q = em.createQuery("select p from Person as p");
    q.setFlushMode(FlushModeType.AUTO);
    try {
        assertNull(q.getSingleResult());
        fail("Should have thrown NoResultException");
    } catch (NoResultException ex) {
    // expected
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException) Person(org.springframework.orm.jpa.domain.Person) EntityNotFoundException(javax.persistence.EntityNotFoundException) NoResultException(javax.persistence.NoResultException) Test(org.junit.Test)

Aggregations

Person (org.springframework.orm.jpa.domain.Person)15 Test (org.junit.Test)12 EntityManager (javax.persistence.EntityManager)8 Query (javax.persistence.Query)8 NoResultException (javax.persistence.NoResultException)4 EntityNotFoundException (javax.persistence.EntityNotFoundException)2 DriversLicense (org.springframework.orm.jpa.domain.DriversLicense)1