Search in sources :

Example 6 with Person

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

the class AbstractContainerEntityManagerFactoryIntegrationTests method testQueryNoPersonsNotTransactional.

@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersonsNotTransactional() {
    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 7 with Person

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

the class AbstractContainerEntityManagerFactoryIntegrationTests method testEntityManagerProxyIsProxy.

@Test
@SuppressWarnings({ "unused", "unchecked" })
public void testEntityManagerProxyIsProxy() {
    assertTrue(Proxy.isProxyClass(sharedEntityManager.getClass()));
    Query q = sharedEntityManager.createQuery("select p from Person as p");
    List<Person> people = q.getResultList();
    assertTrue("Should be open to start with", sharedEntityManager.isOpen());
    sharedEntityManager.close();
    assertTrue("Close should have been silently ignored", sharedEntityManager.isOpen());
}
Also used : Query(javax.persistence.Query) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.Test)

Example 8 with Person

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

the class ApplicationManagedEntityManagerIntegrationTests method doInstantiateAndSave.

protected void doInstantiateAndSave(EntityManager em) {
    testStateClean();
    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 9 with Person

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

the class ApplicationManagedEntityManagerIntegrationTests method testEntityManagerProxyIsProxy.

@Test
@SuppressWarnings("unchecked")
public void testEntityManagerProxyIsProxy() {
    EntityManager em = entityManagerFactory.createEntityManager();
    assertTrue(Proxy.isProxyClass(em.getClass()));
    Query q = em.createQuery("select p from Person as p");
    List<Person> people = q.getResultList();
    assertNotNull(people);
    assertTrue("Should be open to start with", em.isOpen());
    em.close();
    assertFalse("Close should work on application managed EM", em.isOpen());
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.Test)

Example 10 with Person

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

the class ContainerManagedEntityManagerIntegrationTests method testReuseInNewTransaction.

@Test
public void testReuseInNewTransaction() {
    EntityManager em = createContainerManagedEntityManager();
    doInstantiateAndSave(em);
    endTransaction();
    //assertFalse(em.getTransaction().isActive());
    startNewTransaction();
    // Call any method: should cause automatic tx invocation
    assertFalse(em.contains(new Person()));
    //assertTrue(em.getTransaction().isActive());
    doInstantiateAndSave(em);
    setComplete();
    // Should rollback
    endTransaction();
    assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person"));
    // Now clean up the database
    deleteFromTables("person");
}
Also used : EntityManager(javax.persistence.EntityManager) Person(org.springframework.orm.jpa.domain.Person) 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