Search in sources :

Example 11 with Person

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

the class ApplicationManagedEntityManagerIntegrationTests method testReuseInNewTransaction.

@Test
public void testReuseInNewTransaction() {
    EntityManager em = entityManagerFactory.createEntityManager();
    em.joinTransaction();
    doInstantiateAndSave(em);
    endTransaction();
    assertFalse(em.getTransaction().isActive());
    startNewTransaction();
    // Call any method: should cause automatic tx invocation
    assertFalse(em.contains(new Person()));
    assertFalse(em.getTransaction().isActive());
    em.joinTransaction();
    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
    startNewTransaction();
    em.joinTransaction();
    deleteAllPeopleUsingEntityManager(em);
    assertEquals("People have been killed", 0, countRowsInTable(em, "person"));
    setComplete();
}
Also used : EntityManager(javax.persistence.EntityManager) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.Test)

Example 12 with Person

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

the class ContainerManagedEntityManagerIntegrationTests method testEntityManagerProxyIsProxy.

@Test
@SuppressWarnings("unchecked")
public void testEntityManagerProxyIsProxy() {
    EntityManager em = createContainerManagedEntityManager();
    assertTrue(Proxy.isProxyClass(em.getClass()));
    Query q = em.createQuery("select p from Person as p");
    List<Person> people = q.getResultList();
    assertTrue(people.isEmpty());
    assertTrue("Should be open to start with", em.isOpen());
    try {
        em.close();
        fail("Close should not work on container managed EM");
    } catch (IllegalStateException ex) {
    // OK
    }
    assertTrue(em.isOpen());
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.Test)

Example 13 with Person

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

the class AbstractContainerEntityManagerFactoryIntegrationTests method testInstantiateAndSave.

protected void testInstantiateAndSave(EntityManager em) {
    assertEquals("Should be no people from previous transactions", 0, countRowsInTable("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("person"));
}
Also used : Person(org.springframework.orm.jpa.domain.Person)

Example 14 with Person

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

the class AbstractContainerEntityManagerFactoryIntegrationTests method testGetReferenceWhenNoRow.

@Test
public void testGetReferenceWhenNoRow() {
    try {
        Person notThere = sharedEntityManager.getReference(Person.class, 666);
        // We may get here (as with Hibernate).
        // Either behaviour is valid: throw exception on first access
        // or on getReference itself.
        notThere.getFirstName();
        fail("Should have thrown an EntityNotFoundException");
    } catch (EntityNotFoundException ex) {
    // expected
    }
}
Also used : EntityNotFoundException(javax.persistence.EntityNotFoundException) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.Test)

Example 15 with Person

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

the class AbstractContainerEntityManagerFactoryIntegrationTests method testLazyLoading.

@Test
public void testLazyLoading() {
    try {
        Person tony = new Person();
        tony.setFirstName("Tony");
        tony.setLastName("Blair");
        tony.setDriversLicense(new DriversLicense("8439DK"));
        sharedEntityManager.persist(tony);
        setComplete();
        endTransaction();
        startNewTransaction();
        sharedEntityManager.clear();
        Person newTony = entityManagerFactory.createEntityManager().getReference(Person.class, tony.getId());
        assertNotSame(newTony, tony);
        endTransaction();
        assertNotNull(newTony.getDriversLicense());
        newTony.getDriversLicense().getSerialNumber();
    } finally {
        deleteFromTables("person", "drivers_license");
    }
}
Also used : DriversLicense(org.springframework.orm.jpa.domain.DriversLicense) 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