Search in sources :

Example 1 with VersionLockingPolicy

use of org.eclipse.persistence.descriptors.VersionLockingPolicy in project eclipselink by eclipse-ee4j.

the class VersionLockingPolicyStoreInObjectTest method setup.

@Override
protected void setup() {
    getSession().getIdentityMapAccessor().initializeAllIdentityMaps();
    descriptorToModify = project.getDescriptors().get(Employee.class);
    VersionLockingPolicy lockingTestPolicy = new VersionLockingPolicy();
    lockingTestPolicy.storeInObject();
    lockingTestPolicy.setWriteLockFieldName("VERSION");
    descriptorToModify.setOptimisticLockingPolicy(lockingTestPolicy);
}
Also used : Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) VersionLockingPolicy(org.eclipse.persistence.descriptors.VersionLockingPolicy)

Example 2 with VersionLockingPolicy

use of org.eclipse.persistence.descriptors.VersionLockingPolicy in project eclipselink by eclipse-ee4j.

the class AnimalProject method buildAnimalDescriptor.

/**
 * TopLink generated method.
 * <b>WARNING</b>: This code was generated by an automated tool.
 * Any changes will be lost when the code is re-generated
 */
protected void buildAnimalDescriptor() {
    org.eclipse.persistence.descriptors.RelationalDescriptor descriptor = new org.eclipse.persistence.descriptors.RelationalDescriptor();
    // SECTION: DESCRIPTOR
    descriptor.setJavaClass(Animal.class);
    Vector vector = new Vector();
    vector.addElement("OL_ANIMAL");
    descriptor.setTableNames(vector);
    descriptor.addPrimaryKeyFieldName("OL_ANIMAL.ID");
    // SECTION: PROPERTIES
    descriptor.setIdentityMapClass(org.eclipse.persistence.internal.identitymaps.FullIdentityMap.class);
    descriptor.setSequenceNumberName("OL_ANIMAL");
    descriptor.setSequenceNumberFieldName("ID");
    descriptor.setExistenceChecking("Check cache");
    descriptor.setIdentityMapSize(100);
    descriptor.getInheritancePolicy().setShouldReadSubclasses(true);
    descriptor.getInheritancePolicy().setClassIndicatorFieldName("ANIMAL_TYPE");
    descriptor.getInheritancePolicy().setShouldUseClassNameAsIndicator(true);
    VersionLockingPolicy lockingPolicy = new VersionLockingPolicy();
    lockingPolicy.setWriteLockFieldName("OL_ANIMAL.VERSION");
    lockingPolicy.setIsCascaded(true);
    lockingPolicy.setIsStoredInCache(false);
    descriptor.setOptimisticLockingPolicy(lockingPolicy);
    // SECTION: COPY POLICY
    descriptor.createCopyPolicy("constructor");
    // SECTION: INSTANTIATION POLICY
    descriptor.createInstantiationPolicy("constructor");
    // SECTION: DIRECTTOFIELDMAPPING
    org.eclipse.persistence.mappings.DirectToFieldMapping directtofieldmapping = new org.eclipse.persistence.mappings.DirectToFieldMapping();
    directtofieldmapping.setAttributeName("id");
    directtofieldmapping.setIsReadOnly(false);
    directtofieldmapping.setFieldName("OL_ANIMAL.ID");
    descriptor.addMapping(directtofieldmapping);
    // SECTION: DIRECTTOFIELDMAPPING
    org.eclipse.persistence.mappings.DirectToFieldMapping directtofieldmapping1 = new org.eclipse.persistence.mappings.DirectToFieldMapping();
    directtofieldmapping1.setAttributeName("version");
    directtofieldmapping1.setIsReadOnly(false);
    directtofieldmapping1.setFieldName("OL_ANIMAL.VERSION");
    descriptor.addMapping(directtofieldmapping1);
    // SECTION: ONETOMANYMAPPING
    org.eclipse.persistence.mappings.OneToManyMapping onetomanymapping = new org.eclipse.persistence.mappings.OneToManyMapping();
    onetomanymapping.setAttributeName("appointments");
    onetomanymapping.setIsReadOnly(false);
    onetomanymapping.useTransparentList();
    onetomanymapping.setReferenceClass(VetAppointment.class);
    onetomanymapping.setIsPrivateOwned(true);
    onetomanymapping.addTargetForeignKeyFieldName("OL_VET_APPT.ANIMAL_ID", "OL_ANIMAL.ID");
    descriptor.addMapping(onetomanymapping);
    addDescriptor(descriptor);
}
Also used : VersionLockingPolicy(org.eclipse.persistence.descriptors.VersionLockingPolicy) Vector(java.util.Vector)

Example 3 with VersionLockingPolicy

use of org.eclipse.persistence.descriptors.VersionLockingPolicy in project eclipselink by eclipse-ee4j.

the class ALCTEmployeeProject method buildEmployeeDescriptor.

public ClassDescriptor buildEmployeeDescriptor() {
    RelationalDescriptor descriptor = new RelationalDescriptor();
    descriptor.setJavaClass(ALCTEmployee.class);
    descriptor.addTableName("ALCTEMPLOYEE");
    descriptor.addPrimaryKeyFieldName("ALCTEMPLOYEE.EMP_ID");
    // Descriptor Properties.
    descriptor.useSoftCacheWeakIdentityMap();
    descriptor.setIdentityMapSize(100);
    descriptor.useRemoteSoftCacheWeakIdentityMap();
    descriptor.setRemoteIdentityMapSize(100);
    descriptor.setSequenceNumberFieldName("ALCTEMPLOYEE.EMP_ID");
    descriptor.setSequenceNumberName("EMP_SEQ");
    VersionLockingPolicy lockingPolicy = new VersionLockingPolicy();
    lockingPolicy.setWriteLockFieldName("ALCTEMPLOYEE.VERSION");
    descriptor.setOptimisticLockingPolicy(lockingPolicy);
    descriptor.setAlias("ALCTEmployee");
    // Change Tracking
    descriptor.setObjectChangePolicy(new AttributeChangeTrackingPolicy());
    // Cache Invalidation Policy
    // Query Manager.
    descriptor.getQueryManager().checkCacheForDoesExist();
    descriptor.getQueryManager().setQueryTimeout(0);
    // Named Queries.
    // Event Manager.
    // Mappings.
    DirectToFieldMapping firstNameMapping = new DirectToFieldMapping();
    firstNameMapping.setAttributeName("firstName");
    firstNameMapping.setFieldName("ALCTEMPLOYEE.F_NAME");
    firstNameMapping.setNullValue("");
    descriptor.addMapping(firstNameMapping);
    DirectToFieldMapping idMapping = new DirectToFieldMapping();
    idMapping.setAttributeName("id");
    idMapping.setFieldName("ALCTEMPLOYEE.EMP_ID");
    descriptor.addMapping(idMapping);
    DirectToFieldMapping lastNameMapping = new DirectToFieldMapping();
    lastNameMapping.setAttributeName("lastName");
    lastNameMapping.setFieldName("ALCTEMPLOYEE.L_NAME");
    lastNameMapping.setNullValue("");
    descriptor.addMapping(lastNameMapping);
    DirectToFieldMapping genderMapping = new DirectToFieldMapping();
    genderMapping.setAttributeName("gender");
    genderMapping.setFieldName("ALCTEMPLOYEE.GENDER");
    ObjectTypeConverter genderMappingConverter = new ObjectTypeConverter();
    genderMappingConverter.addConversionValue("F", "Female");
    genderMappingConverter.addConversionValue("M", "Male");
    genderMapping.setConverter(genderMappingConverter);
    descriptor.addMapping(genderMapping);
    AggregateObjectMapping periodMapping = new AggregateObjectMapping();
    periodMapping.setAttributeName("period");
    periodMapping.setReferenceClass(ALCTEmploymentPeriod.class);
    periodMapping.setIsNullAllowed(true);
    periodMapping.addFieldNameTranslation("ALCTEMPLOYEE.END_DATE", "endDate->DIRECT");
    periodMapping.addFieldNameTranslation("ALCTEMPLOYEE.START_DATE", "startDate->DIRECT");
    descriptor.addMapping(periodMapping);
    return descriptor;
}
Also used : DirectToFieldMapping(org.eclipse.persistence.mappings.DirectToFieldMapping) RelationalDescriptor(org.eclipse.persistence.descriptors.RelationalDescriptor) AggregateObjectMapping(org.eclipse.persistence.mappings.AggregateObjectMapping) VersionLockingPolicy(org.eclipse.persistence.descriptors.VersionLockingPolicy) ObjectTypeConverter(org.eclipse.persistence.mappings.converters.ObjectTypeConverter) AttributeChangeTrackingPolicy(org.eclipse.persistence.descriptors.changetracking.AttributeChangeTrackingPolicy)

Example 4 with VersionLockingPolicy

use of org.eclipse.persistence.descriptors.VersionLockingPolicy in project eclipselink by eclipse-ee4j.

the class DatabaseQueryMechanism method updateObjectForWriteWithChangeSet.

/**
 * Update the object.
 * This is used by the unit-of-work update.
 */
public void updateObjectForWriteWithChangeSet() {
    WriteObjectQuery writeQuery = getWriteObjectQuery();
    ObjectChangeSet changeSet = writeQuery.getObjectChangeSet();
    Object object = writeQuery.getObject();
    ClassDescriptor descriptor = getDescriptor();
    DescriptorQueryManager queryManager = descriptor.getQueryManager();
    AbstractSession session = getSession();
    CommitManager commitManager = session.getCommitManager();
    // check for user-defined query
    if (// this is not a user-defined query
    (!writeQuery.isUserDefined()) && // there is a user-defined query
    queryManager.hasUpdateQuery() && isExpressionQueryMechanism()) {
        // this is not a hand-coded call (custom SQL etc.)
        // This must be done here because the user defined update does not use a changeset so it will not be set otherwise
        commitManager.markPreModifyCommitInProgress(object);
        performUserDefinedUpdate();
        return;
    }
    // This must be done after the custom query check, otherwise it will be done twice.
    commitManager.markPreModifyCommitInProgress(object);
    DescriptorEventManager eventManager = descriptor.getEventManager();
    if (changeSet.hasChanges()) {
        // PERF: Avoid events if no listeners.
        if (eventManager.hasAnyEventListeners()) {
            DescriptorEvent event = new DescriptorEvent(DescriptorEventManager.PreUpdateWithChangesEvent, writeQuery);
            eventManager.executeEvent(event);
            // PreUpdateWithChangesEvent listeners may have altered the object - should recalculate the change set.
            UnitOfWorkChangeSet uowChangeSet = (UnitOfWorkChangeSet) ((UnitOfWorkImpl) session).getUnitOfWorkChangeSet();
            if (!uowChangeSet.isChangeSetFromOutsideUOW() && writeQuery.getObjectChangeSet().shouldRecalculateAfterUpdateEvent()) {
                // It is first cleared then re-populated by calculateChanges method.
                if (!descriptor.getObjectChangePolicy().isAttributeChangeTrackingPolicy()) {
                    // clear the change set without clearing the maps keys since they are not alterable by the event
                    // if the map is changed, it will be changed in the owning object and the
                    // change set will be changed there as well.
                    writeQuery.getObjectChangeSet().clear(false);
                }
                if (descriptor.getObjectChangePolicy().calculateChangesForExistingObject(object, uowChangeSet, ((UnitOfWorkImpl) session), descriptor, false) == null) {
                    // calculateChanges returns null in case the changeSet doesn't have changes.
                    // It should be removed from the list of ObjectChangeSets that have changes in uowChangeSet.
                    uowChangeSet.getAllChangeSets().remove(writeQuery.getObjectChangeSet());
                }
            }
        }
    }
    // Verify if deep shallow modify is turned on
    if (writeQuery.shouldCascadeParts()) {
        queryManager.preUpdate(writeQuery);
    }
    // The row must not be built until after preUpdate in case the object reference has changed.
    // For a user defined update in the uow to row must be built twice to check if any update is required.
    writeQuery.setModifyRow(descriptor.getObjectBuilder().buildRowForUpdateWithChangeSet(writeQuery));
    Boolean shouldModifyVersionField = changeSet.shouldModifyVersionField();
    if (!getModifyRow().isEmpty() || shouldModifyVersionField != null || changeSet.hasCmpPolicyForcedUpdate()) {
        // If user defined the entire row is required. Must not be built until change is known.
        if (writeQuery.isUserDefined() || writeQuery.isCallQuery()) {
            writeQuery.setModifyRow(descriptor.getObjectBuilder().buildRow(object, session, WriteType.UNDEFINED));
        }
        OptimisticLockingPolicy lockingPolicy = descriptor.getOptimisticLockingPolicy();
        // Update the write lock field if required.
        if (lockingPolicy != null) {
            lockingPolicy.addLockValuesToTranslationRow(writeQuery);
            // Do not lock an object that has previously been optimistically locked within the RWUoW
            boolean existingOptimisticLock = false;
            if (session instanceof RepeatableWriteUnitOfWork) {
                RepeatableWriteUnitOfWork uow = (RepeatableWriteUnitOfWork) session;
                if (uow.getOptimisticReadLockObjects().get(object) != null && uow.getCumulativeUOWChangeSet() != null && uow.getCumulativeUOWChangeSet().getObjectChangeSetForClone(object) != null) {
                    existingOptimisticLock = true;
                }
            }
            if (!existingOptimisticLock) {
                // or if there is no forced update and modifyRow has modifications
                if ((shouldModifyVersionField != null && shouldModifyVersionField) || !getModifyRow().isEmpty()) {
                    // Update the row with newer lock value.
                    lockingPolicy.updateRowAndObjectForUpdate(writeQuery, object);
                } else if (!shouldModifyVersionField && (lockingPolicy instanceof VersionLockingPolicy)) {
                    // Add the existing write lock value to the for a "read" lock (requires something to update).
                    ((VersionLockingPolicy) lockingPolicy).writeLockValueIntoRow(writeQuery, object);
                }
            }
        }
        if (descriptor.hasSerializedObjectPolicy()) {
            descriptor.getSerializedObjectPolicy().putObjectIntoRow(getModifyRow(), object, session);
        }
        // PERF: Avoid events if no listeners.
        if (eventManager.hasAnyEventListeners()) {
            DescriptorEvent event = new DescriptorEvent(DescriptorEventManager.AboutToUpdateEvent, writeQuery);
            event.setRecord(getModifyRow());
            eventManager.executeEvent(event);
        }
        if (QueryMonitor.shouldMonitor()) {
            QueryMonitor.incrementUpdate(getWriteObjectQuery());
        }
        int rowCount = updateObject();
        if (rowCount < 1) {
            if (session.hasEventManager()) {
                session.getEventManager().noRowsModified(writeQuery, object);
            }
        }
        if (lockingPolicy != null) {
            lockingPolicy.validateUpdate(rowCount, object, writeQuery);
        }
    }
    commitManager.markPostModifyCommitInProgress(object);
    // Verify if deep shallow modify is turned on
    if (writeQuery.shouldCascadeParts()) {
        queryManager.postUpdate(writeQuery);
    }
    if ((descriptor.getHistoryPolicy() != null) && descriptor.getHistoryPolicy().shouldHandleWrites()) {
        descriptor.getHistoryPolicy().postUpdate(writeQuery);
    }
    // PERF: Avoid events if no listeners.
    if (eventManager.hasAnyEventListeners()) {
        eventManager.executeEvent(new DescriptorEvent(DescriptorEventManager.PostUpdateEvent, writeQuery));
    }
}
Also used : DescriptorQueryManager(org.eclipse.persistence.descriptors.DescriptorQueryManager) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) CommitManager(org.eclipse.persistence.internal.sessions.CommitManager) UnitOfWorkChangeSet(org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet) VersionLockingPolicy(org.eclipse.persistence.descriptors.VersionLockingPolicy) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) DescriptorEvent(org.eclipse.persistence.descriptors.DescriptorEvent) WriteObjectQuery(org.eclipse.persistence.queries.WriteObjectQuery) DescriptorEventManager(org.eclipse.persistence.descriptors.DescriptorEventManager) RepeatableWriteUnitOfWork(org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) OptimisticLockingPolicy(org.eclipse.persistence.internal.descriptors.OptimisticLockingPolicy)

Example 5 with VersionLockingPolicy

use of org.eclipse.persistence.descriptors.VersionLockingPolicy in project eclipselink by eclipse-ee4j.

the class ListHolder method descriptor.

public static RelationalDescriptor descriptor() {
    RelationalDescriptor descriptor = new RelationalDescriptor();
    // SECTION: DESCRIPTOR
    descriptor.setJavaClass(ListHolder.class);
    Vector vector = new Vector();
    vector.addElement("OL_HOLDER");
    descriptor.setTableNames(vector);
    descriptor.addPrimaryKeyFieldName("OL_HOLDER.ID");
    // SECTION: PROPERTIES
    descriptor.setIdentityMapClass(org.eclipse.persistence.internal.identitymaps.FullIdentityMap.class);
    descriptor.setSequenceNumberName("OL_HOLDER");
    descriptor.setSequenceNumberFieldName("ID");
    descriptor.setExistenceChecking("Check cache");
    descriptor.setIdentityMapSize(100);
    VersionLockingPolicy lockingPolicy = new VersionLockingPolicy();
    lockingPolicy.setWriteLockFieldName("OL_HOLDER.VERSION");
    lockingPolicy.setIsStoredInCache(false);
    descriptor.setOptimisticLockingPolicy(lockingPolicy);
    // SECTION: COPY POLICY
    descriptor.createCopyPolicy("constructor");
    // SECTION: INSTANTIATION POLICY
    descriptor.createInstantiationPolicy("constructor");
    // SECTION: DIRECTTOFIELDMAPPING
    org.eclipse.persistence.mappings.DirectToFieldMapping directtofieldmapping = new org.eclipse.persistence.mappings.DirectToFieldMapping();
    directtofieldmapping.setAttributeName("id");
    directtofieldmapping.setIsReadOnly(false);
    directtofieldmapping.setFieldName("OL_HOLDER.ID");
    descriptor.addMapping(directtofieldmapping);
    // SECTION: DIRECTTOFIELDMAPPING
    org.eclipse.persistence.mappings.DirectToFieldMapping directtofieldmapping1 = new org.eclipse.persistence.mappings.DirectToFieldMapping();
    directtofieldmapping1.setAttributeName("version");
    directtofieldmapping1.setIsReadOnly(false);
    directtofieldmapping1.setFieldName("OL_HOLDER.VERSION");
    descriptor.addMapping(directtofieldmapping1);
    org.eclipse.persistence.mappings.DirectToFieldMapping directtofieldmapping2 = new org.eclipse.persistence.mappings.DirectToFieldMapping();
    directtofieldmapping2.setAttributeName("description");
    directtofieldmapping2.setIsReadOnly(false);
    directtofieldmapping2.setFieldName("OL_HOLDER.DESCR");
    descriptor.addMapping(directtofieldmapping2);
    // SECTION: ONETOMANYMAPPING
    org.eclipse.persistence.mappings.OneToManyMapping onetomanymapping = new org.eclipse.persistence.mappings.OneToManyMapping();
    onetomanymapping.setAttributeName("items");
    onetomanymapping.setIsReadOnly(false);
    onetomanymapping.useTransparentList();
    onetomanymapping.setListOrderFieldName("OL_ITEM.ITEM_ORDER");
    onetomanymapping.setReferenceClass(ListItem.class);
    onetomanymapping.addTargetForeignKeyFieldName("OL_ITEM.HOLDER_ID", "OL_HOLDER.ID");
    descriptor.addMapping(onetomanymapping);
    return descriptor;
}
Also used : RelationalDescriptor(org.eclipse.persistence.descriptors.RelationalDescriptor) VersionLockingPolicy(org.eclipse.persistence.descriptors.VersionLockingPolicy) Vector(java.util.Vector)

Aggregations

VersionLockingPolicy (org.eclipse.persistence.descriptors.VersionLockingPolicy)19 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)7 Vector (java.util.Vector)4 RelationalDescriptor (org.eclipse.persistence.descriptors.RelationalDescriptor)4 OptimisticLockingPolicy (org.eclipse.persistence.internal.descriptors.OptimisticLockingPolicy)4 DescriptorEvent (org.eclipse.persistence.descriptors.DescriptorEvent)2 DescriptorEventManager (org.eclipse.persistence.descriptors.DescriptorEventManager)2 DescriptorQueryManager (org.eclipse.persistence.descriptors.DescriptorQueryManager)2 TimestampLockingPolicy (org.eclipse.persistence.descriptors.TimestampLockingPolicy)2 IntegrityChecker (org.eclipse.persistence.exceptions.IntegrityChecker)2 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)2 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)2 CommitManager (org.eclipse.persistence.internal.sessions.CommitManager)2 RepeatableWriteUnitOfWork (org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork)2 AggregateObjectMapping (org.eclipse.persistence.mappings.AggregateObjectMapping)2 DirectToFieldMapping (org.eclipse.persistence.mappings.DirectToFieldMapping)2 ObjectTypeConverter (org.eclipse.persistence.mappings.converters.ObjectTypeConverter)2 WriteObjectQuery (org.eclipse.persistence.queries.WriteObjectQuery)2 LockTimeoutException (jakarta.persistence.LockTimeoutException)1 PersistenceException (jakarta.persistence.PersistenceException)1