protected void changeObject() { UnitOfWork uow = (UnitOfWork) getSession(); Employee cloneEmployee = (Employee) workingCopy; for (int i = 0; i < managedEmployeesToAdd.size(); i++) { Employee cloneEmployeeToAdd = (Employee) uow.registerObject(managedEmployeesToAdd.get(i)); cloneEmployee.addManagedEmployee(cloneEmployeeToAdd); } for (int i = 0; i < managedEmployeesToRemove.size(); i++) { Employee cloneEmployeeToRemove = (Employee) uow.registerObject(managedEmployeesToRemove.get(i)); cloneEmployee.removeManagedEmployee(cloneEmployeeToRemove); } for (int i = 0; i < phonesToRemove.size(); i++) { PhoneNumber clonePhoneToRemove = (PhoneNumber) uow.registerObject(phonesToRemove.get(i)); cloneEmployee.removePhoneNumber(clonePhoneToRemove); } for (int i = 0; i < phonesToAdd.size(); i++) { PhoneNumber clonePhoneToAdd = (PhoneNumber) uow.registerObject(phonesToAdd.get(i)); cloneEmployee.addPhoneNumber(clonePhoneToAdd); } }
public void test() { // setup Employee manager = new Employee(); manager.setFirstName("Manager"); Employee employee = new Employee(); employee.setFirstName("Employee"); UnitOfWork uow = getSession().acquireUnitOfWork(); uow.registerObject(manager); uow.registerObject(employee); uow.commit(); Vector pk = new Vector(1); pk.add(manager.getId()); version[0] = ((Long) getSession() .getDescriptor(Employee.class) .getOptimisticLockingPolicy() .getWriteLockValue(manager, pk, getAbstractSession())) .longValue(); // test1 - add managed employee, manager's version should increment. uow = getSession().acquireUnitOfWork(); Employee managerClone = (Employee) uow.registerObject(manager); Employee employeeClone = (Employee) uow.registerObject(employee); managerClone.addManagedEmployee(employeeClone); uow.commit(); version[1] = ((Long) getSession() .getDescriptor(Employee.class) .getOptimisticLockingPolicy() .getWriteLockValue(manager, pk, getAbstractSession())) .longValue(); // test2 - alter managed employee, manager's version should NOT increment. uow = getSession().acquireUnitOfWork(); employeeClone = (Employee) uow.registerObject(employee); employeeClone.setFirstName("Altered"); uow.commit(); version[2] = ((Long) getSession() .getDescriptor(Employee.class) .getOptimisticLockingPolicy() .getWriteLockValue(manager, pk, getAbstractSession())) .longValue(); // test3- remove managed employee, manager's version should increment. uow = getSession().acquireUnitOfWork(); managerClone = (Employee) uow.registerObject(manager); employeeClone = (Employee) uow.registerObject(employee); managerClone.removeManagedEmployee(employeeClone); uow.commit(); version[3] = ((Long) getSession() .getDescriptor(Employee.class) .getOptimisticLockingPolicy() .getWriteLockValue(manager, pk, getAbstractSession())) .longValue(); PhoneNumber phone = new PhoneNumber("home", "613", "1111111"); // test4 - add phone, manager's version should increment. uow = getSession().acquireUnitOfWork(); managerClone = (Employee) uow.registerObject(manager); PhoneNumber phoneClone = (PhoneNumber) uow.registerObject(phone); managerClone.addPhoneNumber(phoneClone); uow.commit(); version[4] = ((Long) getSession() .getDescriptor(Employee.class) .getOptimisticLockingPolicy() .getWriteLockValue(manager, pk, getAbstractSession())) .longValue(); // test5- alter phone, manager's version should increment. uow = getSession().acquireUnitOfWork(); phoneClone = (PhoneNumber) uow.registerObject(phone); phoneClone.setType("work"); uow.commit(); version[5] = ((Long) getSession() .getDescriptor(Employee.class) .getOptimisticLockingPolicy() .getWriteLockValue(manager, pk, getAbstractSession())) .longValue(); // test6- remove phone, manager's version should increment. uow = getSession().acquireUnitOfWork(); managerClone = (Employee) uow.registerObject(manager); phoneClone = (PhoneNumber) uow.registerObject(phone); managerClone.removePhoneNumber(phoneClone); uow.commit(); version[6] = ((Long) getSession() .getDescriptor(Employee.class) .getOptimisticLockingPolicy() .getWriteLockValue(manager, pk, getAbstractSession())) .longValue(); }