Search in sources :

Example 1 with CollectionChangeRecord

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);
    }
}
Also used : Order(org.eclipse.persistence.testing.models.jpa.relationships.Order) EntityManager(jakarta.persistence.EntityManager) Customer(org.eclipse.persistence.testing.models.jpa.relationships.Customer) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) UnitOfWorkChangeSet(org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) RepeatableWriteUnitOfWork(org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork) CollectionChangeRecord(org.eclipse.persistence.internal.sessions.CollectionChangeRecord) PersistenceException(jakarta.persistence.PersistenceException) QueryException(org.eclipse.persistence.exceptions.QueryException) NonUniqueResultException(jakarta.persistence.NonUniqueResultException) NoResultException(jakarta.persistence.NoResultException)

Example 2 with CollectionChangeRecord

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;
    }
}
Also used : UnitOfWorkChangeSet(org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) CollectionChangeRecord(org.eclipse.persistence.internal.sessions.CollectionChangeRecord)

Example 3 with CollectionChangeRecord

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;
    }
}
Also used : UnitOfWorkChangeSet(org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) CollectionChangeRecord(org.eclipse.persistence.internal.sessions.CollectionChangeRecord)

Example 4 with CollectionChangeRecord

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);
        }
    }
}
Also used : UnitOfWorkChangeSet(org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) CollectionChangeRecord(org.eclipse.persistence.internal.sessions.CollectionChangeRecord)

Aggregations

CollectionChangeRecord (org.eclipse.persistence.internal.sessions.CollectionChangeRecord)4 ObjectChangeSet (org.eclipse.persistence.internal.sessions.ObjectChangeSet)4 UnitOfWorkChangeSet (org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet)4 EntityManager (jakarta.persistence.EntityManager)1 NoResultException (jakarta.persistence.NoResultException)1 NonUniqueResultException (jakarta.persistence.NonUniqueResultException)1 PersistenceException (jakarta.persistence.PersistenceException)1 QueryException (org.eclipse.persistence.exceptions.QueryException)1 EntityManagerImpl (org.eclipse.persistence.internal.jpa.EntityManagerImpl)1 RepeatableWriteUnitOfWork (org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork)1 Customer (org.eclipse.persistence.testing.models.jpa.relationships.Customer)1 Order (org.eclipse.persistence.testing.models.jpa.relationships.Order)1