use of org.eclipse.persistence.internal.helper.IdentityHashSet in project eclipselink by eclipse-ee4j.
the class SimpleSerializeFetchGroupTests method copyCascade.
void copyCascade(int cascadeDepth) {
EntityManager em = createEntityManager();
try {
beginTransaction(em);
Query query = em.createQuery("SELECT e FROM Employee e");
List<Employee> employees = query.getResultList();
CopyGroup group = new CopyGroup();
if (cascadeDepth == CopyGroup.NO_CASCADE) {
group.dontCascade();
} else if (cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
// default cascade depth setting
group.cascadePrivateParts();
} else if (cascadeDepth == CopyGroup.CASCADE_ALL_PARTS) {
group.cascadeAllParts();
} else {
fail("Invalid cascadeDepth = " + cascadeDepth);
}
group.setShouldResetPrimaryKey(true);
List<Employee> employeesCopy;
if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
// In this case the objects should be copied one by one - each one using a new CopyGroup.
// That ensures most referenced object are original ones (not copies) -
// the only exception is privately owned objects in CASCADE_PRIVATE_PARTS case.
employeesCopy = new ArrayList(employees.size());
for (Employee emp : employees) {
CopyGroup groupClone = group.clone();
employeesCopy.add((Employee) em.unwrap(JpaEntityManager.class).copy(emp, groupClone));
}
} else {
// cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
// In this case all objects should be copied using a single CopyGroup.
// That ensures identities of the copies:
// for instance if several employees referenced the same project,
// then all copies of these employees will reference the single copy of the project.
employeesCopy = (List) em.unwrap(JpaEntityManager.class).copy(employees, group);
}
// IdentityHashSets will be used to verify copy identities
IdentityHashSet originalEmployees = new IdentityHashSet();
IdentityHashSet copyEmployees = new IdentityHashSet();
IdentityHashSet originalAddresses = new IdentityHashSet();
IdentityHashSet copyAddresses = new IdentityHashSet();
IdentityHashSet originalProjects = new IdentityHashSet();
IdentityHashSet copyProjects = new IdentityHashSet();
IdentityHashSet originalPhones = new IdentityHashSet();
IdentityHashSet copyPhones = new IdentityHashSet();
int size = employees.size();
for (int i = 0; i < size; i++) {
Employee emp = employees.get(i);
Employee empCopy = employeesCopy.get(i);
if (cascadeDepth == CopyGroup.CASCADE_ALL_PARTS) {
originalEmployees.add(emp);
copyEmployees.add(empCopy);
} else {
// cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS
// In this case all Employees referenced by empCopyes are originals (manager and managed employees).
// Therefore if we add here each emp and empCopy to originalEmployees and copyEmployees respectively
// then copyEmployees will always contain all original managers and managed + plus all copies.
// Therefore in this case originalEmployees and copyEmployees will contain only references (managers and managed employees).
}
if (emp.getAddress() == null) {
assertTrue("emp.getAddress() == null, but empCopy.getAddress() != null", empCopy.getAddress() == null);
} else {
originalAddresses.add(emp.getAddress());
copyAddresses.add(empCopy.getAddress());
if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
assertTrue("address has been copied", emp.getAddress() == empCopy.getAddress());
} else {
// cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
assertFalse("address has not been copied", emp.getAddress() == empCopy.getAddress());
}
}
boolean same;
for (Project project : emp.getProjects()) {
originalProjects.add(project);
same = false;
for (Project projectCopy : empCopy.getProjects()) {
copyProjects.add(projectCopy);
if (!same && project == projectCopy) {
same = true;
}
}
if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
assertTrue("project has been copied", same);
} else {
// cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
assertFalse("project has not been copied", same);
}
}
for (Employee managedEmp : emp.getManagedEmployees()) {
originalEmployees.add(managedEmp);
same = false;
for (Employee managedEmpCopy : empCopy.getManagedEmployees()) {
copyEmployees.add(managedEmpCopy);
if (!same && managedEmp == managedEmpCopy) {
same = true;
}
}
if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
assertTrue("managedEmployee has been copied", same);
} else {
// cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
assertFalse("managedEmployee has not been copied", same);
}
}
if (emp.getManager() == null) {
assertTrue("emp.getManager() == null, but empCopy.getManager() != null", empCopy.getManager() == null);
} else {
originalEmployees.add(emp.getManager());
copyEmployees.add(empCopy.getManager());
if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
assertTrue("manager has been copied", emp.getManager() == empCopy.getManager());
} else {
// cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
assertFalse("manager has not been copied", emp.getManager() == empCopy.getManager());
}
}
// phoneNumbers is privately owned
for (PhoneNumber phone : emp.getPhoneNumbers()) {
originalPhones.add(phone);
same = false;
for (PhoneNumber phoneCopy : empCopy.getPhoneNumbers()) {
copyPhones.add(phoneCopy);
if (!same && phone == phoneCopy) {
same = true;
}
}
if (cascadeDepth == CopyGroup.NO_CASCADE) {
assertTrue("phone has been copied", same);
} else {
// cascadeDepth == CopyGroup.CASCADE_ALL_PARTS || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS
assertFalse("phone has not been copied", same);
}
}
}
assertTrue("copyEmployees.size() == " + copyEmployees.size() + "; was expected " + originalEmployees.size(), originalEmployees.size() == copyEmployees.size());
assertTrue("copyAddresses.size() == " + copyAddresses.size() + "; was expected " + originalAddresses.size(), originalAddresses.size() == copyAddresses.size());
assertTrue("copyProjects.size() == " + copyProjects.size() + "; was expected " + originalProjects.size(), originalProjects.size() == copyProjects.size());
assertTrue("copyPhones.size() == " + copyPhones.size() + "; was expected " + originalPhones.size(), originalPhones.size() == copyPhones.size());
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
}
}
use of org.eclipse.persistence.internal.helper.IdentityHashSet in project cuba by cuba-platform.
the class EntityStates method checkLoadedWithView.
/**
* Check that all properties of the view are loaded from DB for the passed entity.
* Throws exception if some property is not loaded.
*
* @param entity entity
* @param view view
* @throws IllegalArgumentException if at least one of properties is not loaded
*/
@SuppressWarnings("unchecked")
public void checkLoadedWithView(Entity entity, View view) {
checkNotNullArgument(entity);
checkNotNullArgument(view);
checkLoadedWithView(entity, view, new IdentityHashSet());
}
use of org.eclipse.persistence.internal.helper.IdentityHashSet in project cuba by cuba-platform.
the class EntityStates method isLoadedWithView.
/**
* Check that all properties of the view are loaded from DB for the passed entity.
*
* @param entity entity
* @param view view name
* @return false if at least one of properties is not loaded
*/
@SuppressWarnings("unchecked")
public boolean isLoadedWithView(Entity entity, View view) {
checkNotNullArgument(entity);
checkNotNullArgument(view);
return isLoadedWithView(entity, view, new IdentityHashSet());
}
use of org.eclipse.persistence.internal.helper.IdentityHashSet in project eclipselink by eclipse-ee4j.
the class UnitOfWorkImpl method addPrivateOwnedObject.
/**
* INTERNAL:
* Add a privately owned object to the privateOwnedObjectsMap.
* The UnitOfWork needs to keep track of privately owned objects in order to
* detect and remove private owned objects which are de-referenced.
*/
public void addPrivateOwnedObject(DatabaseMapping mapping, Object privateOwnedObject) {
// only allow mapped, non-null objects to be added
if (privateOwnedObject != null && getDescriptor(privateOwnedObject) != null) {
Map<DatabaseMapping, Set> privateOwnedObjects = getPrivateOwnedObjects();
Set objectsForMapping = privateOwnedObjects.get(mapping);
if (objectsForMapping == null) {
objectsForMapping = new IdentityHashSet();
privateOwnedObjects.put(mapping, objectsForMapping);
}
objectsForMapping.add(privateOwnedObject);
}
}
use of org.eclipse.persistence.internal.helper.IdentityHashSet in project eclipselink by eclipse-ee4j.
the class SimpleSerializeFetchGroupTests method copyCascade.
void copyCascade(int cascadeDepth) {
EntityManager em = createEntityManager("fieldaccess");
try {
beginTransaction(em);
Query query = em.createQuery("SELECT e FROM Employee e");
List<Employee> employees = query.getResultList();
CopyGroup group = new CopyGroup();
if (cascadeDepth == CopyGroup.NO_CASCADE) {
group.dontCascade();
} else if (cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
// default cascade depth setting
group.cascadePrivateParts();
} else if (cascadeDepth == CopyGroup.CASCADE_ALL_PARTS) {
group.cascadeAllParts();
} else {
fail("Invalid cascadeDepth = " + cascadeDepth);
}
group.setShouldResetPrimaryKey(true);
List<Employee> employeesCopy;
if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
// In this case the objects should be copied one by one - each one using a new CopyGroup.
// That ensures most referenced object are original ones (not copies) -
// the only exception is privately owned objects in CASCADE_PRIVATE_PARTS case.
employeesCopy = new ArrayList(employees.size());
for (Employee emp : employees) {
CopyGroup groupClone = group.clone();
employeesCopy.add((Employee) em.unwrap(JpaEntityManager.class).copy(emp, groupClone));
}
} else {
// cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
// In this case all objects should be copied using a single CopyGroup.
// That ensures identities of the copies:
// for instance if several employees referenced the same project,
// then all copies of these employees will reference the single copy of the project.
employeesCopy = (List) em.unwrap(JpaEntityManager.class).copy(employees, group);
}
// IdentityHashSets will be used to verify copy identities
IdentityHashSet originalEmployees = new IdentityHashSet();
IdentityHashSet copyEmployees = new IdentityHashSet();
IdentityHashSet originalAddresses = new IdentityHashSet();
IdentityHashSet copyAddresses = new IdentityHashSet();
IdentityHashSet originalProjects = new IdentityHashSet();
IdentityHashSet copyProjects = new IdentityHashSet();
IdentityHashSet originalPhones = new IdentityHashSet();
IdentityHashSet copyPhones = new IdentityHashSet();
int size = employees.size();
for (int i = 0; i < size; i++) {
Employee emp = employees.get(i);
Employee empCopy = employeesCopy.get(i);
if (cascadeDepth == CopyGroup.CASCADE_ALL_PARTS) {
originalEmployees.add(emp);
copyEmployees.add(empCopy);
} else {
// cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS
// In this case all Employees referenced by empCopyes are originals (manager and managed employees).
// Therefore if we add here each emp and empCopy to originalEmployees and copyEmployees respectively
// then copyEmployees will always contain all original managers and managed + plus all copies.
// Therefore in this case originalEmployees and copyEmployees will contain only references (managers and managed employees).
}
if (emp.getAddress() == null) {
assertTrue("emp.getAddress() == null, but empCopy.getAddress() != null", empCopy.getAddress() == null);
} else {
originalAddresses.add(emp.getAddress());
copyAddresses.add(empCopy.getAddress());
if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
assertTrue("address has been copied", emp.getAddress() == empCopy.getAddress());
} else {
// cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
assertFalse("address has not been copied", emp.getAddress() == empCopy.getAddress());
}
}
boolean same;
for (Project project : emp.getProjects()) {
originalProjects.add(project);
same = false;
for (Project projectCopy : empCopy.getProjects()) {
copyProjects.add(projectCopy);
if (!same && project == projectCopy) {
same = true;
}
}
if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
assertTrue("project has been copied", same);
} else {
// cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
assertFalse("project has not been copied", same);
}
}
for (Employee managedEmp : emp.getManagedEmployees()) {
originalEmployees.add(managedEmp);
same = false;
for (Employee managedEmpCopy : empCopy.getManagedEmployees()) {
copyEmployees.add(managedEmpCopy);
if (!same && managedEmp == managedEmpCopy) {
same = true;
}
}
if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
assertTrue("managedEmployee has been copied", same);
} else {
// cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
assertFalse("managedEmployee has not been copied", same);
}
}
if (emp.getManager() == null) {
assertTrue("emp.getManager() == null, but empCopy.getManager() != null", empCopy.getManager() == null);
} else {
originalEmployees.add(emp.getManager());
copyEmployees.add(empCopy.getManager());
if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
assertTrue("manager has been copied", emp.getManager() == empCopy.getManager());
} else {
// cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
assertFalse("manager has not been copied", emp.getManager() == empCopy.getManager());
}
}
// phoneNumbers is privately owned
for (PhoneNumber phone : emp.getPhoneNumbers()) {
originalPhones.add(phone);
same = false;
for (PhoneNumber phoneCopy : empCopy.getPhoneNumbers()) {
copyPhones.add(phoneCopy);
if (!same && phone == phoneCopy) {
same = true;
}
}
if (cascadeDepth == CopyGroup.NO_CASCADE) {
assertTrue("phone has been copied", same);
} else {
// cascadeDepth == CopyGroup.CASCADE_ALL_PARTS || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS
assertFalse("phone has not been copied", same);
}
}
}
assertTrue("copyEmployees.size() == " + copyEmployees.size() + "; was expected " + originalEmployees.size(), originalEmployees.size() == copyEmployees.size());
assertTrue("copyAddresses.size() == " + copyAddresses.size() + "; was expected " + originalAddresses.size(), originalAddresses.size() == copyAddresses.size());
assertTrue("copyProjects.size() == " + copyProjects.size() + "; was expected " + originalProjects.size(), originalProjects.size() == copyProjects.size());
assertTrue("copyPhones.size() == " + copyPhones.size() + "; was expected " + originalPhones.size(), originalPhones.size() == copyPhones.size());
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
}
}
Aggregations