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();
}
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());
}
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"));
}
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
}
}
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");
}
}
Aggregations