use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testIgnoreRemovedObjectsOnDatabaseSync.
public void testIgnoreRemovedObjectsOnDatabaseSync() {
EntityManager em = createEntityManager();
beginTransaction(em);
Query phoneQuery = em.createQuery("Select p from PhoneNumber p where p.owner.lastName like 'Dow%'");
Query empQuery = em.createQuery("Select e FROM Employee e where e.lastName like 'Dow%'");
// --setup
try {
Employee emp = new Employee();
emp.setLastName("Dowder");
PhoneNumber phone = new PhoneNumber("work", "613", "5555555");
emp.addPhoneNumber(phone);
phone = new PhoneNumber("home", "613", "4444444");
emp.addPhoneNumber(phone);
Address address = new Address("SomeStreet", "somecity", "province", "country", "postalcode");
emp.setAddress(address);
em.persist(emp);
em.flush();
emp = new Employee();
emp.setLastName("Dows");
phone = new PhoneNumber("work", "613", "2222222");
emp.addPhoneNumber(phone);
phone = new PhoneNumber("home", "613", "1111111");
emp.addPhoneNumber(phone);
address = new Address("street1", "city1", "province1", "country1", "postalcode1");
emp.setAddress(address);
em.persist(emp);
em.flush();
// --end setup
List<Employee> emps = empQuery.getResultList();
List phones = phoneQuery.getResultList();
for (Iterator iterator = phones.iterator(); iterator.hasNext(); ) {
em.remove(iterator.next());
}
em.flush();
for (Iterator<Employee> iterator = emps.iterator(); iterator.hasNext(); ) {
em.remove(iterator.next());
}
} catch (RuntimeException ex) {
rollbackTransaction(em);
throw ex;
}
try {
em.flush();
} catch (IllegalStateException ex) {
rollbackTransaction(em);
closeEntityManager(em);
em = createEntityManager();
beginTransaction(em);
try {
phoneQuery = em.createQuery("Select p from PhoneNumber p where p.owner.lastName like 'Dow%'");
empQuery = em.createQuery("Select e from Employee e where e.lastName like 'Dow%'");
List<Employee> emps = empQuery.getResultList();
List phones = phoneQuery.getResultList();
for (Iterator iterator = phones.iterator(); iterator.hasNext(); ) {
em.remove(iterator.next());
}
for (Iterator<Employee> iterator = emps.iterator(); iterator.hasNext(); ) {
em.remove(iterator.next());
}
commitTransaction(em);
} catch (RuntimeException re) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
throw re;
}
fail("Failed to ignore the removedobject when cascading on database sync");
}
commitTransaction(em);
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testMultipleExistenceChecksForDetachedObjects.
public void testMultipleExistenceChecksForDetachedObjects() {
EntityManager em = createEntityManager();
Department dept1 = new Department("Dept1");
Department dept2 = new Department("Dept2");
Department dept3 = new Department("DetachedDept");
Employee emp = new Employee("Bob", "Bob");
try {
List<Address> results = em.createQuery("Select a from Address a").getResultList();
beginTransaction(em);
em.persist(emp);
em.persist(dept1);
em.persist(dept2);
commitTransaction(em);
em.clear();
beginTransaction(em);
emp = em.find(Employee.class, emp.getId());
emp.setDepartment(dept2);
em.flush();
em.persist(dept3);
emp.setDepartment(dept3);
em.flush();
emp.setDepartment(dept1);
em.flush();
commitTransaction(em);
closeEntityManager(em);
verifyObjectInCacheAndDatabase(emp);
} finally {
try {
em = createEntityManager();
beginTransaction(em);
em.remove(em.find(Department.class, dept1.getId()));
em.remove(em.find(Department.class, dept2.getId()));
em.remove(em.find(Department.class, dept3.getId()));
em.remove(em.find(Employee.class, emp.getId()));
commitTransaction(em);
} catch (Exception e) {
// ignore
}
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testMergeNewObject.
// bug6167431: tests calling merge on a new object puts it in the cache, and that all new objects in the tree get IDs generated
public void testMergeNewObject() {
// create an Employee
Employee emp = new Employee();
emp.setFirstName("testMergeNewObjectEmployee");
emp.setAddress(new Address("45 O'Connor", "Ottawa", "Ont", "Canada", "K1P1A4"));
// persist the Employee
EntityManager em = createEntityManager();
try {
beginTransaction(em);
Employee managedEmp = em.merge(emp);
assertNotNull("merged Employee doesn't have its ID generated", managedEmp.getId());
assertNotNull("merged Employee cannot be found using find", em.find(Employee.class, managedEmp.getId()));
// this won't work till bug:6193761 is fixed
// this.assertTrue("referenced Address doesn't have its ID generated", managedEmp.getAddress().getId()!=0);
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testRefreshInvalidateDeletedObject.
// Bug5955326, refresh should invalidate the shared cached object that was deleted outside of JPA.
public void testRefreshInvalidateDeletedObject() {
EntityManager em1 = createEntityManager();
EntityManager em2 = createEntityManager();
Address address = new Address();
address.setCity("Kanata");
// persist the Address
try {
// Ensure shared cache being used.
boolean isIsolated = ((EntityManagerImpl) em1).getServerSession().getClassDescriptorForAlias("Address").isIsolated();
if (isIsolated) {
throw new Exception("This test should use non-isolated cache setting class descriptor for test.");
}
beginTransaction(em1);
em1.persist(address);
commitTransaction(em1);
// Cache the Address
em1 = createEntityManager();
beginTransaction(em1);
address = em1.find(Address.class, address.getID());
// Delete Address outside of JPA so that the object still stored in the cache.
em2 = createEntityManager();
beginTransaction(em2);
em2.createNativeQuery("DELETE FROM CMP3_ADDRESS where ADDRESS_ID = ?1").setParameter(1, address.getID()).executeUpdate();
commitTransaction(em2);
// Call refresh to invalidate the object
em1.refresh(address);
} catch (Exception e) {
// expected exception
} finally {
if (isTransactionActive(em1)) {
rollbackTransaction(em1);
}
}
// Verify
beginTransaction(em1);
address = em1.find(Address.class, address.getID());
commitTransaction(em1);
assertNull("The deleted object is still valid in share cache", address);
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testNoPersistOnCommit.
// Test Not using the persist operation on commit.
public void testNoPersistOnCommit() {
// Properties only works in jse.
if (isOnServer()) {
return;
}
EntityManager em = createEntityManager();
beginTransaction(em);
Employee employee = new Employee();
employee.setLastName("SomeName");
Address addr = new Address();
addr.setCity("Douglas");
try {
em.persist(employee);
commitTransaction(em);
verifyObjectInCacheAndDatabase(employee);
closeEntityManager(em);
em = createEntityManager();
beginTransaction(em);
((RepeatableWriteUnitOfWork) JpaHelper.getEntityManager(em).getUnitOfWork()).setDiscoverUnregisteredNewObjectsWithoutPersist(true);
employee = em.find(Employee.class, employee.getId());
employee.setAddress(addr);
addr.getEmployees().add(employee);
commitTransaction(em);
verifyObjectInCacheAndDatabase(employee);
closeEntityManager(em);
em = createEntityManager();
clearCache();
beginTransaction(em);
((RepeatableWriteUnitOfWork) JpaHelper.getEntityManager(em).getUnitOfWork()).setDiscoverUnregisteredNewObjectsWithoutPersist(true);
employee = em.find(Employee.class, employee.getId());
employee.getAddress().setCountry("country");
employee.getAddress().getEmployees().size();
employee.setAddress((Address) null);
em.remove(employee);
commitTransaction(em);
em = createEntityManager();
beginTransaction(em);
employee = em.find(Employee.class, employee.getId());
assertNull("Employee Not Deleted", employee);
commitTransaction(em);
closeEntityManager(em);
} catch (RuntimeException exception) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw exception;
} finally {
try {
em = createEntityManager();
clearCache();
beginTransaction(em);
em.remove(em.find(Address.class, addr.getID()));
commitTransaction(em);
} catch (RuntimeException ex) {
// ignore
}
}
}
Aggregations