/** * set the key and value fields that are used to build the container from database rows */ protected void initializeContainerPolicy(AbstractSession session) { ((DirectMapContainerPolicy)getContainerPolicy()).setKeyField(getDirectKeyField()); ((DirectMapContainerPolicy)getContainerPolicy()).setValueField(getDirectField()); ((DirectMapContainerPolicy)getContainerPolicy()).setKeyConverter(getKeyConverter()); ((DirectMapContainerPolicy)getContainerPolicy()).setValueConverter(getValueConverter()); }
/** * INTERNAL: * Compare the attributes belonging to this mapping for the objects. */ public boolean compareObjects(Object firstObject, Object secondObject, AbstractSession session) { Object firstObjectMap = getRealCollectionAttributeValueFromObject(firstObject, session); Object secondObjectMap = getRealCollectionAttributeValueFromObject(secondObject, session); DirectMapContainerPolicy mapContainerPolicy = (DirectMapContainerPolicy)getContainerPolicy(); return mapContainerPolicy.compareContainers(firstObjectMap, secondObjectMap); }
/** * The field name on the reference table is initialized and cached. */ protected void initializeDirectKeyField(AbstractSession session) throws DescriptorException { if (getDirectKeyField() == null) { throw DescriptorException.directFieldNameNotSet(this); } getDirectKeyField().setTable(getReferenceTable()); getDirectKeyField().setIndex(1); }
if (isReadOnly()) { return; objects = getRealCollectionAttributeValueFromObject(query.getObject(), query.getSession()); DirectMapContainerPolicy containerPolicy = (DirectMapContainerPolicy)getContainerPolicy(); if (containerPolicy.isEmpty(objects)) { return; prepareTranslationRow(query.getTranslationRow(), query.getObject(), query.getSession()); for (int index = 0; index < getReferenceKeyFields().size(); index++) { DatabaseField referenceKey = (DatabaseField)getReferenceKeyFields().elementAt(index); DatabaseField sourceKey = (DatabaseField)getSourceKeyFields().elementAt(index); Object sourceKeyValue = query.getTranslationRow().get(sourceKey); databaseRow.put(referenceKey, sourceKeyValue); Object key = containerPolicy.next(keyIter, query.getSession()); Object value = containerPolicy.valueFromKey(key, objects); if (getKeyConverter() != null) { key = getKeyConverter().convertObjectValueToDataValue(key, query.getSession()); if (getValueConverter() != null) { value = getValueConverter().convertObjectValueToDataValue(value, query.getSession()); databaseRow.put(getDirectKeyField(), key); databaseRow.put(getDirectField(), value); event[1] = getInsertQuery(); event[2] = databaseRow.clone();
DirectMapChangeRecord changeRecord = (DirectMapChangeRecord)changeSet.getChangesForAttributeNamed(this.getAttributeName()); if (changeRecord == null){ return; for (int index = 0; index < getReferenceKeyFields().size(); index++) { DatabaseField referenceKey = (DatabaseField)getReferenceKeyFields().elementAt(index); DatabaseField sourceKey = (DatabaseField)getSourceKeyFields().elementAt(index); Object sourceKeyValue = writeQuery.getTranslationRow().get(sourceKey); writeQuery.getTranslationRow().put(referenceKey, sourceKeyValue); Object key = iterator.next(); AbstractRecord thisRow = (AbstractRecord)writeQuery.getTranslationRow().clone(); if (getKeyConverter() != null){ key = getKeyConverter().convertObjectValueToDataValue(key, writeQuery.getSession()); thisRow.add(getDirectKeyField(), key); event[1] = getDeleteQuery(); event[2] = thisRow; writeQuery.getSession().getCommitManager().addDataModificationEvent(this, event); AbstractRecord thisRow = (AbstractRecord)writeQuery.getTranslationRow().clone(); Object value = changeRecord.getAddObjects().get(key); if (getKeyConverter() != null){ key = getKeyConverter().convertObjectValueToDataValue(key, writeQuery.getSession()); if (getValueConverter() != null){ value = getValueConverter().convertObjectValueToDataValue(value, writeQuery.getSession());
if (isTargetUnInitialized) { if (mergeManager.shouldMergeWorkingCopyIntoOriginal() && (!isAttributeValueInstantiated(source))) { setAttributeValueInObject(target, getIndirectionPolicy().getOriginalIndirectionObject(getAttributeValueFromObject(source), mergeManager.getSession())); return; if (!shouldMergeCascadeReference(mergeManager)) { if (!isAttributeValueInstantiated(target)) { } else if (!isAttributeValueInstantiated(source)) { Map valueOfSource = (Map)getRealCollectionAttributeValueFromObject(source, mergeManager.getSession()); DirectMapContainerPolicy containerPolicy = (DirectMapContainerPolicy)getContainerPolicy(); Object valueOfTarget = getRealCollectionAttributeValueFromObject(target, mergeManager.getSession()); Object newContainer = containerPolicy.containerInstance(containerPolicy.sizeFor(valueOfSource)); setRealAttributeValueInObject(target, valueOfTarget);
ContainerPolicy mappingContainerPolicy = getContainerPolicy(); synchronized (query) { referenceDataByKey = (Hashtable)query.getProperty("batched objects"); mappingContainerPolicy = getContainerPolicy(); if (referenceDataByKey == null) { Vector rows = (Vector)session.executeQuery(query, argumentRow); Object referenceKey = referenceRow.get(getDirectKeyField()); Object referenceValue = referenceRow.get(getDirectField()); CacheKey eachCacheKey = new CacheKey(extractKeyFromReferenceRow(referenceRow, session)); if (getKeyConverter() != null) { referenceKey = getKeyConverter().convertDataValueToObjectValue(referenceKey, query.getSession()); if (getValueConverter() != null) { referenceValue = getValueConverter().convertDataValueToObjectValue(referenceValue, query.getSession()); Object result = referenceDataByKey.get(new CacheKey(extractPrimaryKeyFromRow(databaseRow, session)));
protected void initializeDeleteQuery(AbstractSession session) { if (!getDeleteQuery().hasSessionName()) { getDeleteQuery().setSessionName(session.getName()); } if (hasCustomDeleteQuery()) { return; } Expression builder = new ExpressionBuilder(); Expression directKeyExp = builder.getField(getDirectKeyField()).equal(builder.getParameter(getDirectKeyField())); Expression expression = null; SQLDeleteStatement statement = new SQLDeleteStatement(); // Construct an expression to delete from the relation table. for (int index = 0; index < getReferenceKeyFields().size(); index++) { DatabaseField referenceKey = (DatabaseField)getReferenceKeyFields().elementAt(index); DatabaseField sourceKey = (DatabaseField)getSourceKeyFields().elementAt(index); Expression subExp1 = builder.getField(referenceKey); Expression subExp2 = builder.getParameter(sourceKey); Expression subExpression = subExp1.equal(subExp2); expression = subExpression.and(expression); } expression = expression.and(directKeyExp); statement.setWhereClause(expression); statement.setTable(getReferenceTable()); getDeleteQuery().setSQLStatement(statement); }
protected void initializeSelectionStatement(AbstractSession session) { SQLSelectStatement statement = new SQLSelectStatement(); statement.addTable(getReferenceTable()); statement.addField((DatabaseField)getDirectField().clone()); statement.addField((DatabaseField)getDirectKeyField().clone()); statement.setWhereClause(getSelectionCriteria()); statement.normalize(session, null); getSelectionQuery().setSQLStatement(statement); }
Object backUpAttribute = null; DirectMapContainerPolicy cp = (DirectMapContainerPolicy)getContainerPolicy(); cloneAttribute = getAttributeValueFromObject(clone); if ((cloneAttribute != null) && (!getIndirectionPolicy().objectIsInstantiated(cloneAttribute))) { return null; Map cloneObjectCollection = (Map)getRealCollectionAttributeValueFromObject(clone, session); HashMap originalKeyValues = new HashMap(10); HashMap cloneKeyValues = new HashMap(10); backUpAttribute = getAttributeValueFromObject(backUp); if ((backUpAttribute == null) && (cloneAttribute == null)) { return null; Map backUpCollection = (Map)getRealCollectionAttributeValueFromObject(backUp, session); Object backUpIter = cp.iteratorFor(backUpCollection); while (cp.hasNext(backUpIter)) {// Make a lookup of the objects changeRecord.setAttribute(getAttributeName()); changeRecord.setMapping(this); changeRecord.addAdditionChange(cloneKeyValues);
DirectMapContainerPolicy containerPolicy = (DirectMapContainerPolicy)getContainerPolicy(); Map valueOfTarget = null; AbstractSession session = mergeManager.getSession(); if ((isAttributeValueInstantiated(target)) && (!changeRecord.getOwner().isNew())) { valueOfTarget = (Map)getRealCollectionAttributeValueFromObject(target, session); } else { if (!isAttributeValueInstantiated(target)) { if (mergeManager.shouldMergeChangesIntoDistributedCache()) { return; Object valueOfSource = getRealCollectionAttributeValueFromObject(source, session); for (Object iterator = containerPolicy.iteratorFor(valueOfSource); containerPolicy.hasNext(iterator);) { setRealAttributeValueInObject(target, valueOfTarget);
/** * INTERNAL: * Require for cloning, the part must be cloned. * Ignore the objects, use the attribute value. */ public Object buildCloneForPartObject(Object attributeValue, Object original, Object clone, UnitOfWorkImpl unitOfWork, boolean isExisting) { DirectMapContainerPolicy containerPolicy = (DirectMapContainerPolicy)getContainerPolicy(); if (attributeValue == null) { return containerPolicy.containerInstance(1); } Object clonedAttributeValue = containerPolicy.containerInstance(containerPolicy.sizeFor(attributeValue)); // I need to synchronize here to prevent the collection from changing while I am cloning it. // This will occur when I am merging into the cache and I am instantiating a UOW valueHolder at the same time // I can not synchronize around the clone, as this will cause deadlocks, so I will need to copy the collection then create the clones // I will use a temporary collection to help speed up the process Object temporaryCollection = null; synchronized (attributeValue) { temporaryCollection = containerPolicy.cloneFor(attributeValue); } for (Object keysIterator = containerPolicy.iteratorFor(temporaryCollection); containerPolicy.hasNext(keysIterator);) { Object key = containerPolicy.next(keysIterator, unitOfWork); Object cloneKey = buildKeyClone(key, unitOfWork, isExisting); Object cloneValue = buildElementClone(containerPolicy.valueFromKey(key, temporaryCollection), unitOfWork, isExisting); containerPolicy.addInto(cloneKey, cloneValue, clonedAttributeValue, unitOfWork); } return clonedAttributeValue; }
/** * Initialize insert query. This query is used to insert the collection of objects into the * reference table. */ protected void initializeInsertQuery(AbstractSession session) { super.initializeInsertQuery(session); getInsertQuery().getModifyRow().put(getDirectKeyField(), null); }
/** * INTERNAL: * Remove a value and its change set from the collection change record. This is used by * attribute change tracking. */ public void removeFromCollectionChangeRecord(Object newKey, Object newValue, ObjectChangeSet objectChangeSet, UnitOfWorkImpl uow) throws DescriptorException { DirectMapChangeRecord collectionChangeRecord = (DirectMapChangeRecord)objectChangeSet.getChangesForAttributeNamed(this.getAttributeName()); if (collectionChangeRecord == null) { collectionChangeRecord = new DirectMapChangeRecord(objectChangeSet); collectionChangeRecord.setAttribute(getAttributeName()); collectionChangeRecord.setMapping(this); objectChangeSet.addChange(collectionChangeRecord); } collectionChangeRecord.addRemoveChange(newKey, newValue); }
/** * Build direct collection table definitions in a TopLink desciptor */ private void buildDirectCollectionTableDefinition(DirectCollectionMapping mapping, ClassDescriptor desc) { //first create direct collection table TableDefinition tblDef = getTableDefFromDBTable(mapping.getReferenceTable()); DatabaseField dbField = null; //add the table reference key(s) Vector refPkFields = mapping.getReferenceKeyFields(); for (int index = 0; index < refPkFields.size(); index++) { dbField = resolveDatabaseField((DatabaseField) refPkFields.get(index), (DatabaseField) mapping.getSourceKeyFields().get(index)); tblDef.addField(getDirectCollectionReferenceKeyFieldDefFromDBField(dbField)); } //add the direct collection field to the table. tblDef.addField(getFieldDefFromDBField(mapping.getDirectField(), false)); //if the mapping is direct-map field, add the direct key field to the table as well. if (mapping.isDirectMapMapping()) { dbField = ((DirectMapMapping) mapping).getDirectKeyField(); tblDef.addField(getFieldDefFromDBField(dbField, false)); } }
/** * INTERNAL: * Add a new value and its change set to the collection change record. This is used by * attribute change tracking. If a value has changed then issue a remove first with the key * then an add. */ public void addToCollectionChangeRecord(Object newKey, Object newValue, ObjectChangeSet objectChangeSet, UnitOfWorkImpl uow) throws DescriptorException { DirectMapChangeRecord collectionChangeRecord = (DirectMapChangeRecord)objectChangeSet.getChangesForAttributeNamed(this.getAttributeName()); if (collectionChangeRecord == null) { collectionChangeRecord = new DirectMapChangeRecord(objectChangeSet); collectionChangeRecord.setAttribute(getAttributeName()); collectionChangeRecord.setMapping(this); objectChangeSet.addChange(collectionChangeRecord); } collectionChangeRecord.addAdditionChange(newKey, newValue); }
/** * ADVANCED: * This method is used to have an object removed from a collection once the changeSet is applied * The referenceKey parameter should only be used for direct Maps. */ public void simpleRemoveFromCollectionChangeRecord(Object referenceKey, Object objectToRemove, ObjectChangeSet changeSet, AbstractSession session) { DirectMapChangeRecord collectionChangeRecord = (DirectMapChangeRecord)changeSet.getChangesForAttributeNamed(getAttributeName()); if (collectionChangeRecord == null) { collectionChangeRecord = new DirectMapChangeRecord(changeSet); collectionChangeRecord.setAttribute(getAttributeName()); collectionChangeRecord.setMapping(this); collectionChangeRecord.getRemoveObjects().put(referenceKey, objectToRemove); changeSet.addChange(collectionChangeRecord); } else { if (collectionChangeRecord.getAddObjects().containsKey(referenceKey)) { collectionChangeRecord.getAddObjects().remove(referenceKey); } else { collectionChangeRecord.getRemoveObjects().put(referenceKey, objectToRemove); } } }
/** * ADVANCED: * This method is used to have an object add to a collection once the changeSet is applied * The referenceKey parameter should only be used for direct Maps. */ public void simpleAddToCollectionChangeRecord(Object referenceKey, Object objectToAdd, ObjectChangeSet changeSet, AbstractSession session) { DirectMapChangeRecord collectionChangeRecord = (DirectMapChangeRecord)changeSet.getChangesForAttributeNamed(getAttributeName()); if (collectionChangeRecord == null) { collectionChangeRecord = new DirectMapChangeRecord(changeSet); collectionChangeRecord.setAttribute(getAttributeName()); collectionChangeRecord.setMapping(this); collectionChangeRecord.getAddObjects().put(referenceKey, objectToAdd); changeSet.addChange(collectionChangeRecord); } else { if (collectionChangeRecord.getRemoveObjects().containsKey(referenceKey)) { collectionChangeRecord.getRemoveObjects().remove(referenceKey); } else { collectionChangeRecord.getAddObjects().put(referenceKey, objectToAdd); } } }
/** * INTERNAL: * Either create a new change record or update the change record with the new value. * This is used by attribute change tracking. */ public void updateChangeRecord(Object clone, Object newValue, Object oldValue, ObjectChangeSet objectChangeSet, UnitOfWorkImpl uow) throws DescriptorException { DirectMapChangeRecord collectionChangeRecord = (DirectMapChangeRecord)objectChangeSet.getChangesForAttributeNamed(this.getAttributeName()); if (collectionChangeRecord == null) { collectionChangeRecord = new DirectMapChangeRecord(objectChangeSet); collectionChangeRecord.setAttribute(getAttributeName()); collectionChangeRecord.setMapping(this); objectChangeSet.addChange(collectionChangeRecord); } if (collectionChangeRecord.getOriginalCollection() == null){ collectionChangeRecord.setOriginalCollection(oldValue); } collectionChangeRecord.setLatestCollection(newValue); objectChangeSet.deferredDetectionRequiredOn(getAttributeName()); }