use of org.eclipse.persistence.internal.sessions.CollectionChangeRecord in project eclipselink by eclipse-ee4j.
the class RelationshipModelJUnitTestSuite method testChangeSetForNewObject.
// Bug 357103
public void testChangeSetForNewObject() {
EntityManager em = createEntityManager();
beginTransaction(em);
Customer cust = new Customer();
cust.setName("Joe");
em.persist(cust);
Order order = new Order();
order.setQuantity(1);
em.persist(order);
cust.addOrder(order);
commitTransaction(em);
RepeatableWriteUnitOfWork uow = null;
try {
beginTransaction(em);
cust = em.find(Customer.class, cust.getCustomerId());
Order order2 = new Order();
order2.setQuantity(2);
order2.setShippingAddress("123 Main St.");
em.persist(order2);
cust.addOrder(order2);
EntityManagerImpl impl = (EntityManagerImpl) JpaHelper.getEntityManager(em);
uow = impl.getActivePersistenceContext(null);
em.flush();
UnitOfWorkChangeSet uowChangeSet = uow.getCumulativeUOWChangeSet();
ObjectChangeSet customerChangeSet = uowChangeSet.getCloneToObjectChangeSet().get(cust);
CollectionChangeRecord orderChangeRecord = (CollectionChangeRecord) customerChangeSet.getChangesForAttributeNamed("orders");
Iterator<ObjectChangeSet> i = orderChangeRecord.getAddObjectList().keySet().iterator();
while (i.hasNext()) {
ObjectChangeSet orderChangeSet = i.next();
assertTrue("There are changes in the change set. There should be no changes for a new object.", orderChangeSet.getChanges().isEmpty());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rollbackTransaction(em);
beginTransaction(em);
cust = em.find(Customer.class, cust.getCustomerId());
em.remove(cust);
commitTransaction(em);
}
}
use of org.eclipse.persistence.internal.sessions.CollectionChangeRecord in project eclipselink by eclipse-ee4j.
the class ListContainerPolicy method updateChangeRecordForSelfMerge.
/**
* INTERNAL:
* Update a ChangeRecord to replace the ChangeSet for the old entity with the changeSet for the new Entity. This is
* used when an Entity is merged into itself and the Entity reference new or detached entities.
*/
@Override
public void updateChangeRecordForSelfMerge(ChangeRecord changeRecord, Object source, Object target, ForeignReferenceMapping mapping, UnitOfWorkChangeSet parentUOWChangeSet, UnitOfWorkImpl unitOfWork) {
Map<ObjectChangeSet, ObjectChangeSet> list = ((CollectionChangeRecord) changeRecord).getAddObjectList();
ObjectChangeSet sourceSet = parentUOWChangeSet.getCloneToObjectChangeSet().get(source);
if (list.containsKey(sourceSet)) {
ObjectChangeSet targetSet = ((UnitOfWorkChangeSet) unitOfWork.getUnitOfWorkChangeSet()).findOrCreateLocalObjectChangeSet(target, mapping.getReferenceDescriptor(), unitOfWork.isCloneNewObject(target));
parentUOWChangeSet.addObjectChangeSetForIdentity(targetSet, target);
list.remove(sourceSet);
list.put(targetSet, targetSet);
return;
}
List<ObjectChangeSet> overFlow = ((CollectionChangeRecord) changeRecord).getAddOverFlow();
int index = 0;
for (ObjectChangeSet changeSet : overFlow) {
if (changeSet == sourceSet) {
overFlow.set(index, ((UnitOfWorkChangeSet) unitOfWork.getUnitOfWorkChangeSet()).findOrCreateLocalObjectChangeSet(target, mapping.getReferenceDescriptor(), unitOfWork.isCloneNewObject(target)));
return;
}
++index;
}
}
use of org.eclipse.persistence.internal.sessions.CollectionChangeRecord in project eclipselink by eclipse-ee4j.
the class ContainerPolicy method updateChangeRecordForSelfMerge.
/**
* INTERNAL:
* Update a ChangeRecord to replace the ChangeSet for the old entity with the changeSet for the new Entity. This is
* used when an Entity is merged into itself and the Entity reference new or detached entities.
*/
public void updateChangeRecordForSelfMerge(ChangeRecord changeRecord, Object source, Object target, ForeignReferenceMapping mapping, UnitOfWorkChangeSet parentUOWChangeSet, UnitOfWorkImpl unitOfWork) {
Map<ObjectChangeSet, ObjectChangeSet> list = ((CollectionChangeRecord) changeRecord).getAddObjectList();
ObjectChangeSet sourceSet = parentUOWChangeSet.getCloneToObjectChangeSet().get(source);
if (list.containsKey(sourceSet)) {
ObjectChangeSet targetSet = ((UnitOfWorkChangeSet) unitOfWork.getUnitOfWorkChangeSet()).findOrCreateLocalObjectChangeSet(target, mapping.getReferenceDescriptor(), unitOfWork.isCloneNewObject(target));
targetSet.setNewKey(sourceSet.getNewKey());
targetSet.setOldKey(sourceSet.getOldKey());
parentUOWChangeSet.addObjectChangeSetForIdentity(targetSet, target);
list.remove(sourceSet);
list.put(targetSet, targetSet);
return;
}
}
use of org.eclipse.persistence.internal.sessions.CollectionChangeRecord in project eclipselink by eclipse-ee4j.
the class UnidirectionalOneToManyMapping method postCalculateChanges.
/**
* INTERNAL:
* Overridden by mappings that require additional processing of the change record after the record has been calculated.
*/
@Override
public void postCalculateChanges(org.eclipse.persistence.sessions.changesets.ChangeRecord changeRecord, UnitOfWorkImpl uow) {
// targets are added to and/or removed to/from the source.
CollectionChangeRecord collectionChangeRecord = (CollectionChangeRecord) changeRecord;
Iterator<ObjectChangeSet> it = collectionChangeRecord.getAddObjectList().values().iterator();
while (it.hasNext()) {
ObjectChangeSet change = it.next();
if (!change.hasChanges()) {
change.setShouldModifyVersionField(Boolean.TRUE);
((org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet) change.getUOWChangeSet()).addObjectChangeSet(change, uow, false);
}
}
// in the mapping is privately owned then the target will be deleted - no need to modify target version.
it = collectionChangeRecord.getRemoveObjectList().values().iterator();
while (it.hasNext()) {
ObjectChangeSet change = it.next();
if (!isPrivateOwned()) {
if (!change.hasChanges()) {
change.setShouldModifyVersionField(Boolean.TRUE);
((org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet) change.getUOWChangeSet()).addObjectChangeSet(change, uow, false);
}
} else {
containerPolicy.postCalculateChanges(change, referenceDescriptor, this, uow);
}
}
}
Aggregations