PlatformTransactionManager txMgr = (PlatformTransactionManager) applicationContext.getBean("transactionManager");
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
TransactionStatus txStatus = txMgr.getTransaction(def);
emp = new Employee();
emp.setSalary(Long.valueOf(100));
emp.setName("Sample Employee");
Address address = new Address();
address.setCity("San Francisco");
address.setStreet("One Market St. #300");
address.setEmployee(emp);
PhoneNumber office = PhoneNumber.init(emp, "415-555-0000", Phonetype.OFFICE);
office.setEmployee(emp);
PhoneNumber cell = PhoneNumber.init(emp, "415-555-1111", Phonetype.CELL);
cell.setEmployee(emp);
EmploymentPeriod empPer = new EmploymentPeriod();
empPer.setStartDate(JPATestUtils.getCalendar(false).getTime());
empPer.setEndDate(JPATestUtils.getCalendar(false).getTime());
emp.setEmploymentPeriod(empPer);
Project p1 = new GovernmentProject();
p1.setName("GovernmentProject");
Project p2 = new CovertProject("classA");
p2.setName("CovertProject");
ProjectEmployee pe1 = new ProjectEmployee();
pe1.setEmployee(emp);
pe1.setProject(p1);
ProjectEmployee pe2 = new ProjectEmployee();
pe2.setEmployee(emp);
pe2.setProject(p2);
//TODO create another employee and assign to govt. and verify collection type props
entityManager.persist(emp);
entityManager.persist(p2);
entityManager.persist(pe2);
entityManager.persist(p1);
entityManager.persist(pe1);
entityManager.persist(office);
entityManager.persist(cell);
entityManager.persist(address);
txMgr.commit(txStatus);
Metamodel m = entityManager.getMetamodel();
EntityType<Employee> etype = m.entity(Employee.class); //deliberately a little verbose here
Query q = entityManager.createQuery("Select t From " + etype.getName() + " t");
@SuppressWarnings("rawtypes")
List results = q.getResultList();
Employee employee = (Employee) results.iterator().next();
Assert.assertNotNull(employee.getId(), employee.getClass().getName() + " ID was not generated.");
Assert.assertTrue(entityManager.contains(employee), "contains() failed.");
txStatus = txMgr.getTransaction(def);
Employee empFromDb = entityManager.find(Employee.class, employee.getId());
Assert.assertNotNull(empFromDb,
"The entity was not stored to the database.");
Assert.assertEquals(empFromDb.getEmploymentPeriod().getStartDate(), employee.getEmploymentPeriod().getStartDate(),
"Start date not same");
// Now we need to make sure update works on embedded entities
Calendar endDate = JPATestUtils.getCalendar(false);
endDate.add(Calendar.DAY_OF_YEAR, 90);
empFromDb.getEmploymentPeriod().setEndDate(endDate.getTime());
entityManager.merge(empFromDb);
txMgr.commit(txStatus);
entityManager.clear();
// Read back upated object and make sure endDate is updated
empFromDb = entityManager.find(Employee.class, employee.getId());
Assert.assertEquals(empFromDb.getEmploymentPeriod().getEndDate(), endDate.getTime(),
"End date not same");
}