/** * The selection criteria are created with target foreign keys and source "primary" keys. * These criteria are then used to read the target records from the table. * These criteria are also used as the default "delete all" criteria. * * CR#3922 - This method is almost the same as buildSelectionCriteria() the difference * is that TargetForeignKeysToSourceKeys contains more information after login then SourceKeyFields * contains before login. */ protected Expression buildDefaultSelectionCriteriaAndAddFieldsToQuery() { Expression selectionCriteria = null; Expression builder = new ExpressionBuilder(); for (Iterator keys = getTargetForeignKeysToSourceKeys().keySet().iterator(); keys.hasNext();) { DatabaseField targetForeignKey = (DatabaseField)keys.next(); DatabaseField sourceKey = getTargetForeignKeysToSourceKeys().get(targetForeignKey); Expression partialSelectionCriteria = builder.getField(targetForeignKey).equal(builder.getParameter(sourceKey)); selectionCriteria = partialSelectionCriteria.and(selectionCriteria); } getContainerPolicy().addAdditionalFieldsToQuery(getSelectionQuery(), builder); return selectionCriteria; }
/** * The selection criteria are created with target foreign keys and source "primary" keys. * These criteria are then used to read the target records from the table. * These criteria are also used as the default "delete all" criteria. * * CR#3922 - This method is almost the same as buildSelectionCriteria() the difference * is that TargetForeignKeysToSourceKeys contains more information after login then SourceKeyFields * contains before login. */ protected Expression buildDefaultSelectionCriteriaAndAddFieldsToQuery() { Expression selectionCriteria = null; Expression builder = new ExpressionBuilder(); for (Iterator keys = getTargetForeignKeysToSourceKeys().keySet().iterator(); keys.hasNext();) { DatabaseField targetForeignKey = (DatabaseField)keys.next(); DatabaseField sourceKey = getTargetForeignKeysToSourceKeys().get(targetForeignKey); Expression partialSelectionCriteria = builder.getField(targetForeignKey).equal(builder.getParameter(sourceKey)); selectionCriteria = partialSelectionCriteria.and(selectionCriteria); } getContainerPolicy().addAdditionalFieldsToQuery(getSelectionQuery(), builder); return selectionCriteria; }
/** * The selection criteria are created with target foreign keys and source "primary" keys. * These criteria are then used to read the target records from the table. * These criteria are also used as the default "delete all" criteria. * * CR#3922 - This method is almost the same as buildSelectionCriteria() the difference * is that TargetForeignKeysToSourceKeys contains more information after login then SourceKeyFields * contains before login. */ protected Expression buildDefaultSelectionCriteriaAndAddFieldsToQuery() { Expression selectionCriteria = null; Expression builder = new ExpressionBuilder(); for (Iterator keys = getTargetForeignKeysToSourceKeys().keySet().iterator(); keys.hasNext();) { DatabaseField targetForeignKey = (DatabaseField)keys.next(); DatabaseField sourceKey = getTargetForeignKeysToSourceKeys().get(targetForeignKey); Expression partialSelectionCriteria = builder.getField(targetForeignKey).equal(builder.getParameter(sourceKey)); selectionCriteria = partialSelectionCriteria.and(selectionCriteria); } getContainerPolicy().addAdditionalFieldsToQuery(getSelectionQuery(), builder); return selectionCriteria; }
if (shouldInitializeSelectionCriteria() || requiresDataModificationEvents() || getContainerPolicy().requiresDataModificationEvents()) { throw DescriptorException.noTargetForeignKeysSpecified(this); } else {
/** * We need to execute the batch query and store the * results in a hash table keyed by the (primary) keys * of the source objects. */ protected Hashtable executeBatchQuery(DatabaseQuery query, AbstractSession session, AbstractRecord row) { ContainerPolicy mappingCP = this.getContainerPolicy(); ContainerPolicy queryCP = ((ReadAllQuery)query).getContainerPolicy(); Object queryResult = null; queryResult = session.executeQuery(query, row); Hashtable batchedObjects = new Hashtable(queryCP.sizeFor(queryResult)); for (Object iter = queryCP.iteratorFor(queryResult); queryCP.hasNext(iter);) { Object eachReferenceObject = queryCP.next(iter, session); CacheKey eachForeignKey = new CacheKey(extractForeignKeyFromReferenceObject(eachReferenceObject, session)); Object container = batchedObjects.get(eachForeignKey); if (container == null) { container = mappingCP.containerInstance(); batchedObjects.put(eachForeignKey, container); } mappingCP.addInto(eachReferenceObject, container, session); } return batchedObjects; }
/** * This method will make sure that all the records privately owned by this mapping are * actually removed. If such records are found then those are all read and removed one * by one along with their privately owned parts. */ protected void deleteReferenceObjectsLeftOnDatabase(DeleteObjectQuery query) throws DatabaseException, OptimisticLockException { Object objects = readPrivateOwnedForObject(query); // Delete all these object one by one. ContainerPolicy cp = getContainerPolicy(); for (Object iter = cp.iteratorFor(objects); cp.hasNext(iter);) { query.getSession().deleteObject(cp.next(iter, query.getSession())); } }
/** * INTERNAL: * Used to verify whether the specified object is deleted or not. */ public boolean verifyDelete(Object object, AbstractSession session) throws DatabaseException { if (this.isPrivateOwned()) { Object objects = this.getRealCollectionAttributeValueFromObject(object, session); ContainerPolicy containerPolicy = getContainerPolicy(); for (Object iter = containerPolicy.iteratorFor(objects); containerPolicy.hasNext(iter);) { if (!session.verifyDelete(containerPolicy.next(iter, session))) { return false; } } } return true; } }
/** * This method will make sure that all the records privately owned by this mapping are * actually removed. If such records are found then those are all read and removed one * by one along with their privately owned parts. */ protected void deleteReferenceObjectsLeftOnDatabase(DeleteObjectQuery query) throws DatabaseException, OptimisticLockException { Object objects = readPrivateOwnedForObject(query); // Delete all these object one by one. ContainerPolicy cp = getContainerPolicy(); for (Object iter = cp.iteratorFor(objects); cp.hasNext(iter);) { query.getSession().deleteObject(cp.next(iter, query.getSession())); } }
/** * This method will make sure that all the records privately owned by this mapping are * actually removed. If such records are found then those are all read and removed one * by one along with their privately owned parts. */ protected void deleteReferenceObjectsLeftOnDatabase(DeleteObjectQuery query) throws DatabaseException, OptimisticLockException { Object objects = readPrivateOwnedForObject(query); // Delete all these object one by one. ContainerPolicy cp = getContainerPolicy(); for (Object iter = cp.iteratorFor(objects); cp.hasNext(iter);) { query.getSession().deleteObject(cp.next(iter, query.getSession())); } }
/** * INTERNAL: * Used to verify whether the specified object is deleted or not. */ @Override public boolean verifyDelete(Object object, AbstractSession session) throws DatabaseException { if (this.isPrivateOwned() || isCascadeRemove()) { Object objects = getRealCollectionAttributeValueFromObject(object, session); ContainerPolicy containerPolicy = getContainerPolicy(); for (Object iter = containerPolicy.iteratorFor(objects); containerPolicy.hasNext(iter);) { if (!session.verifyDelete(containerPolicy.next(iter, session))) { return false; } } } return true; }
/** * INTERNAL: * Used to verify whether the specified object is deleted or not. */ @Override public boolean verifyDelete(Object object, AbstractSession session) throws DatabaseException { if (this.isPrivateOwned() || isCascadeRemove()) { Object objects = getRealCollectionAttributeValueFromObject(object, session); ContainerPolicy containerPolicy = getContainerPolicy(); for (Object iter = containerPolicy.iteratorFor(objects); containerPolicy.hasNext(iter);) { if (!session.verifyDelete(containerPolicy.next(iter, session))) { return false; } } } return true; }
/** * INTERNAL: * Extract the value from the batch optimized query. */ public Object extractResultFromBatchQuery(DatabaseQuery query, AbstractRecord row, AbstractSession session, AbstractRecord argumentRow) { if(((ReadAllQuery)query).shouldIncludeData()) { return super.extractResultFromBatchQuery(query, row, session, argumentRow); } //this can be null, because either one exists in the query or it will be created Hashtable batchedObjects = null; synchronized (query) { batchedObjects = getBatchReadObjects(query, session); if (batchedObjects == null) { batchedObjects = executeBatchQuery(query, session, argumentRow); setBatchReadObjects(batchedObjects, query, session); query.setSession(null); } } Object result = batchedObjects.get(new CacheKey(extractPrimaryKeyFromRow(row, session))); // The source object might not have any target objects if (result == null) { return getContainerPolicy().containerInstance(); } else { return result; } }
/** * Initialize and set the descriptor for the referenced class in this mapping. * Added here initialization of target foreign keys and target primary keys so that they are ready when * CollectionMapping.initialize initializes listOrderField. */ protected void initializeReferenceDescriptor(AbstractSession session) throws DescriptorException { super.initializeReferenceDescriptor(session); if (!isSourceKeySpecified()) { // sourceKeyFields will be empty when #setTargetForeignKeyFieldName() is used setSourceKeyFields(org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(getDescriptor().getPrimaryKeyFields())); } initializeTargetForeignKeysToSourceKeys(); if(requiresDataModificationEvents() || getContainerPolicy().requiresDataModificationEvents()) { initializeTargetPrimaryKeyFields(); } }
/** * INTERNAL: * Update target foreign key after a target object was added to the source. This follows following steps. * <p>- Extract primary key and its value from the source object. * <p>- Extract target key and its value from the target object. * <p>- Construct an update statement with above fields and values for target table. * <p>- execute the statement. */ public void updateTargetForeignKeyPostUpdateSource_ObjectAdded(ObjectLevelModifyQuery query, Object objectAdded, Map extraData) throws DatabaseException { if (isReadOnly() || addTargetQuery == null) { return; } ContainerPolicy cp = getContainerPolicy(); prepareTranslationRow(query.getTranslationRow(), query.getObject(), query.getDescriptor(), query.getSession()); AbstractRecord databaseRow = buildKeyRowForTargetUpdate(query); // Extract target field and its value. Construct insert statement and execute it int size = targetPrimaryKeyFields.size(); for (int index = 0; index < size; index++) { DatabaseField targetPrimaryKey = targetPrimaryKeyFields.get(index); Object targetKeyValue = getReferenceDescriptor().getObjectBuilder().extractValueFromObjectForField(cp.unwrapIteratorResult(objectAdded), targetPrimaryKey, query.getSession()); databaseRow.put(targetPrimaryKey, targetKeyValue); } ContainerPolicy.copyMapDataToRow(cp.getKeyMappingDataForWriteQuery(objectAdded, query.getSession()), databaseRow); if(listOrderField != null && extraData != null) { databaseRow.put(listOrderField, extraData.get(listOrderField)); } query.getSession().executeQuery(addTargetQuery, databaseRow); }
/** * INTERNAL: * Update target foreign key after a target object was added to the source. This follows following steps. * <p>- Extract primary key and its value from the source object. * <p>- Extract target key and its value from the target object. * <p>- Construct an update statement with above fields and values for target table. * <p>- execute the statement. */ public void updateTargetForeignKeyPostUpdateSource_ObjectAdded(ObjectLevelModifyQuery query, Object objectAdded, Map extraData) throws DatabaseException { if (isReadOnly() || addTargetQuery == null) { return; } ContainerPolicy cp = getContainerPolicy(); prepareTranslationRow(query.getTranslationRow(), query.getObject(), query.getDescriptor(), query.getSession()); AbstractRecord databaseRow = buildKeyRowForTargetUpdate(query); // Extract target field and its value. Construct insert statement and execute it int size = targetPrimaryKeyFields.size(); for (int index = 0; index < size; index++) { DatabaseField targetPrimaryKey = targetPrimaryKeyFields.get(index); Object targetKeyValue = getReferenceDescriptor().getObjectBuilder().extractValueFromObjectForField(cp.unwrapIteratorResult(objectAdded), targetPrimaryKey, query.getSession()); databaseRow.put(targetPrimaryKey, targetKeyValue); } ContainerPolicy.copyMapDataToRow(cp.getKeyMappingDataForWriteQuery(objectAdded, query.getSession()), databaseRow); if(listOrderField != null && extraData != null) { databaseRow.put(listOrderField, extraData.get(listOrderField)); } query.getSession().executeQuery(addTargetQuery, databaseRow); }
/** * INTERNAL: * Initialize the mapping. */ public void initialize(AbstractSession session) throws DescriptorException { super.initialize(session); getContainerPolicy().initialize(session, getReferenceDescriptor().getDefaultTable()); if (shouldInitializeSelectionCriteria()) { setSelectionCriteria(buildDefaultSelectionCriteriaAndAddFieldsToQuery()); } initializeDeleteAllQuery(); if(requiresDataModificationEvents() || getContainerPolicy().requiresDataModificationEvents()) { initializeAddTargetQuery(session); initializeRemoveTargetQuery(session); initializeRemoveAllTargetsQuery(session); } if (getReferenceDescriptor() != null && getReferenceDescriptor().hasTablePerClassPolicy()) { // This will do nothing if we have already prepared for this // source mapping or if the source mapping does not require // any special prepare logic. getReferenceDescriptor().getTablePerClassPolicy().prepareChildrenSelectionQuery(this, session); } }
/** * Initialize and set the descriptor for the referenced class in this mapping. * Added here initialization of target foreign keys and target primary keys so that they are ready when * CollectionMapping.initialize initializes listOrderField. */ @Override protected void initializeReferenceDescriptor(AbstractSession session) throws DescriptorException { super.initializeReferenceDescriptor(session); if (!isSourceKeySpecified()) { // sourceKeyFields will be empty when #setTargetForeignKeyFieldName() is used setSourceKeyFields(org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(getDescriptor().getPrimaryKeyFields())); } initializeTargetForeignKeysToSourceKeys(); if (usesIndirection()) { for (DatabaseField field : getSourceKeyFields()) { field.setKeepInRow(true); } } if(requiresDataModificationEvents() || getContainerPolicy().requiresDataModificationEvents()) { initializeTargetPrimaryKeyFields(); } }
/** * Initialize and set the descriptor for the referenced class in this mapping. * Added here initialization of target foreign keys and target primary keys so that they are ready when * CollectionMapping.initialize initializes listOrderField. */ protected void initializeReferenceDescriptor(AbstractSession session) throws DescriptorException { super.initializeReferenceDescriptor(session); if (!isSourceKeySpecified()) { // sourceKeyFields will be empty when #setTargetForeignKeyFieldName() is used setSourceKeyFields(org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(getDescriptor().getPrimaryKeyFields())); } initializeTargetForeignKeysToSourceKeys(); if (usesIndirection()) { for (DatabaseField field : getSourceKeyFields()) { field.setKeepInRow(true); } } if(requiresDataModificationEvents() || getContainerPolicy().requiresDataModificationEvents()) { initializeTargetPrimaryKeyFields(); } }
/** * Delete all the reference objects with a single query. */ protected void deleteAll(DeleteObjectQuery query) throws DatabaseException { Object attribute = getAttributeAccessor().getAttributeValueFromObject(query.getObject()); if(usesIndirection()) { if(attribute == null || !getIndirectionPolicy().objectIsInstantiated(attribute)) { // An empty Vector indicates to DeleteAllQuery that no objects should be removed from cache ((DeleteAllQuery)getDeleteAllQuery()).executeDeleteAll(query.getSession().getSessionForClass(getReferenceClass()), query.getTranslationRow(), new Vector(0)); return; } } Object referenceObjects = this.getRealCollectionAttributeValueFromObject(query.getObject(), query.getSession()); ((DeleteAllQuery)getDeleteAllQuery()).executeDeleteAll(query.getSession().getSessionForClass(getReferenceClass()), query.getTranslationRow(), getContainerPolicy().vectorFor(referenceObjects, query.getSession())); }
ContainerPolicy cp = getContainerPolicy(); Object objects = getRealCollectionAttributeValueFromObject(query.getObject(), query.getSession()); if (cp.isEmpty(objects)) {