public void initialize(AbstractSession session) throws DescriptorException { if (session.hasBroker()) { if (getReferenceClass() == null) { throw DescriptorException.referenceClassNotSpecified(this); session = session.getBroker().getSessionForClass(getReferenceClass()); getContainerPolicy().initialize(session, getReferenceDescriptor().getDefaultTable()); if (shouldInitializeSelectionCriteria()) { setSelectionCriteria(buildDefaultSelectionCriteriaAndAddFieldsToQuery()); initializeDeleteAllQuery(session); if (requiresDataModificationEvents() || getContainerPolicy().requiresDataModificationEvents()) { initializeAddTargetQuery(session); initializeRemoveTargetQuery(session); initializeRemoveAllTargetsQuery(session); if (getDescriptor().getTables().size() > 1) { DatabaseTable firstTable = getDescriptor().getTables().get(0); for (DatabaseField field : getSourceKeyFields()) { if (!field.getTable().equals(firstTable)) { getDescriptor().setHasMultipleTableConstraintDependecy(true);
if (getTargetForeignKeyFields().isEmpty()) { if (shouldInitializeSelectionCriteria() || requiresDataModificationEvents() || getContainerPolicy().requiresDataModificationEvents()) { throw DescriptorException.noTargetForeignKeysSpecified(this); } else { if (getTargetForeignKeyFields().size() != getSourceKeyFields().size()) { throw DescriptorException.targetForeignKeysSizeMismatch(this); for (int index = 0; index < getTargetForeignKeyFields().size(); index++) { DatabaseField field = getReferenceDescriptor().buildField(getTargetForeignKeyFields().get(index)); getTargetForeignKeyFields().set(index, field); for (int index = 0; index < getSourceKeyFields().size(); index++) { DatabaseField field = getDescriptor().buildField(getSourceKeyFields().get(index)); getSourceKeyFields().set(index, field); Iterator<DatabaseField> targetForeignKeys = getTargetForeignKeyFields().iterator(); Iterator<DatabaseField> sourceKeys = getSourceKeyFields().iterator(); while (targetForeignKeys.hasNext()) { DatabaseField targetForeignKey = targetForeignKeys.next(); DatabaseField sourcePrimaryKey = sourceKeys.next(); getTargetForeignKeysToSourceKeys().put(targetForeignKey, sourcePrimaryKey); getSourceKeysToTargetForeignKeys().put(sourcePrimaryKey, targetForeignKey);
/** * Return a new one to many mapping for this type of descriptor. */ public CollectionMapping newOneToManyMapping() { return new OneToManyMapping(); }
OneToManyMapping mapping = (OneToManyMapping) descriptor.getMappingForAttributeName( "someRelationship" ); Expression origExp = mapping.buildSelectionCriteria(); ExpressionBuilder expBuilder = origExp.getBuilder(); Expression constantExp = expBuilder.get( "someAttribute" ).in( new String[] { "V", "W", "U" } ); Expression newExp = origExp.and( constantExp ); mapping.setSelectionCriteria( newExp );
/** * Initialize the delete all query. * This query is used to delete the collection of objects from the * database. */ protected void initializeDeleteAllQuery() { ((DeleteAllQuery)getDeleteAllQuery()).setReferenceClass(getReferenceClass()); if (!hasCustomDeleteAllQuery()) { // the selection criteria are re-used by the delete all query if (getSelectionCriteria() == null) { getDeleteAllQuery().setSelectionCriteria(buildDefaultSelectionCriteriaAndAddFieldsToQuery()); } else { getDeleteAllQuery().setSelectionCriteria(getSelectionCriteria()); } } }
/** * Prepare a cascade locking policy. */ @Override public void prepareCascadeLockingPolicy() { CascadeLockingPolicy policy = new CascadeLockingPolicy(getDescriptor(), getReferenceDescriptor()); policy.setQueryKeyFields(getSourceKeysToTargetForeignKeys()); getReferenceDescriptor().addCascadeLockingPolicy(policy); }
/** * 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()) { return; } ContainerPolicy cp = getContainerPolicy(); prepareTranslationRow(query.getTranslationRow(), query.getObject(), 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); } }
if (isReadOnly()) { return; if (shouldObjectModifyCascadeToParts(query) && !query.shouldCascadeOnlyDependentParts()) { Object objects = getRealCollectionAttributeValueFromObject(query.getObject(), query.getSession()); ContainerPolicy cp = getContainerPolicy(); for (Object iter = cp.iteratorFor(objects); cp.hasNext(iter);) { Object wrappedObject = cp.nextEntry(iter, query.getSession()); Object object = cp.unwrapIteratorResult(wrappedObject); if (isPrivateOwned()) { if (requiresDataModificationEvents() || getContainerPolicy().requiresDataModificationEvents()) { if ((requiresDataModificationEvents() || containerPolicy.shouldUpdateForeignKeysPostInsert())) { if (shouldDeferInsert()) { query.getSession().getCommitManager().addDataModificationEvent(this, event); } else { ContainerPolicy cp = getContainerPolicy(); Object objects = getRealCollectionAttributeValueFromObject(query.getObject(), query.getSession()); if (cp.isEmpty(objects)) { return; prepareTranslationRow(query.getTranslationRow(), query.getObject(), query.getDescriptor(), query.getSession()); AbstractRecord keyRow = buildKeyRowForTargetUpdate(query); ClassDescriptor referenceDesc = getReferenceDescriptor();
/** * Initialize the delete all query. * This query is used to delete the collection of objects from the * database. */ protected void initializeDeleteAllQuery(AbstractSession session) { ((DeleteAllQuery)getDeleteAllQuery()).setReferenceClass(getReferenceClass()); getDeleteAllQuery().setName(getAttributeName()); ((DeleteAllQuery)getDeleteAllQuery()).setIsInMemoryOnly(isCascadeOnDeleteSetOnDatabase()); if (!hasCustomDeleteAllQuery()) { // the selection criteria are re-used by the delete all query if (getSelectionCriteria() == null) { getDeleteAllQuery().setSelectionCriteria(buildDefaultSelectionCriteriaAndAddFieldsToQuery()); } else { getDeleteAllQuery().setSelectionCriteria(getSelectionCriteria()); } } if (!getDeleteAllQuery().hasSessionName()) { getDeleteAllQuery().setSessionName(session.getName()); } if (getDeleteAllQuery().getPartitioningPolicy() == null) { getDeleteAllQuery().setPartitioningPolicy(getPartitioningPolicy()); } }
OneToManyMapping mapping = new OneToManyMapping(); mapping.setAttributeName(name); mapping.setReferenceClass(refType.getJavaClass()); mapping.addTargetForeignKeyFieldName(fkFieldNames[index], targetField); mapping.useTransparentList();
/** * 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())); }
if (requiresDataModificationEvents() || containerPolicy.requiresDataModificationEvents()){ if (shouldDeferInsert()) { query.getSession().getCommitManager().addDataModificationEvent(this, event); } else { ContainerPolicy cp = getContainerPolicy(); prepareTranslationRow(query.getTranslationRow(), query.getObject(), query.getDescriptor(), query.getSession()); AbstractRecord keyRow = buildKeyRowForTargetUpdate(query); ClassDescriptor referenceDesc = getReferenceDescriptor(); AbstractSession session = query.getSession(); InsertObjectQuery insertQuery = getInsertObjectQuery(session, referenceDesc); insertQuery.setObject(objectAdded); insertQuery.setCascadePolicy(query.getCascadePolicy()); updateTargetForeignKeyPostUpdateSource_ObjectAdded(query, objectAdded, extraData);
/** * Delete all the reference objects with a single query. */ protected void deleteAll(DeleteObjectQuery query, AbstractSession session) throws DatabaseException { Object attribute = getAttributeValueFromObject(query.getObject()); if (usesIndirection()) { if (!this.indirectionPolicy.objectIsInstantiated(attribute)) { // An empty Vector indicates to DeleteAllQuery that no objects should be removed from cache ((DeleteAllQuery)this.deleteAllQuery).executeDeleteAll(session.getSessionForClass(this.referenceClass), query.getTranslationRow(), new Vector(0)); return; } } Object referenceObjects = getRealCollectionAttributeValueFromObject(query.getObject(), session); // PERF: Avoid delete if empty. if (session.isUnitOfWork() && this.containerPolicy.isEmpty(referenceObjects)) { return; } ((DeleteAllQuery)this.deleteAllQuery).executeDeleteAll(session.getSessionForClass(getReferenceClass()), query.getTranslationRow(), this.containerPolicy.vectorFor(referenceObjects, session)); }
/** * INTERNAL: */ protected void prepareOneToManySelectionQuery(OneToManyMapping sourceMapping, AbstractSession session) { // Clone the mapping because in reality that is what we have, that // is, a 1-M mapping to each class of the hierarchy. OneToManyMapping oneToMany = (OneToManyMapping) sourceMapping.clone(); // Update the foreign key fields on the mapping. Basically, take the // table name off and let the descriptor figure it out. Vector<DatabaseField> targetForeignKeyFields = new Vector<DatabaseField>(); for (DatabaseField fkField : oneToMany.getTargetForeignKeysToSourceKeys().keySet()) { targetForeignKeyFields.add(new DatabaseField(fkField.getName())); } // Update our foreign key fields and clear the key maps. oneToMany.setTargetForeignKeyFields(targetForeignKeyFields); oneToMany.getTargetForeignKeysToSourceKeys().clear(); oneToMany.getSourceKeysToTargetForeignKeys().clear(); addSelectionQuery(oneToMany, sourceMapping, session); }
OneToManyMapping mapping = new OneToManyMapping(); process(mapping); mapping.addTargetForeignKeyField(fkField, pkField); mapping.setMappedBy(getMappedBy());
if (databaseMapping instanceof OneToManyMapping) { OneToManyMapping mapping = (OneToManyMapping)databaseMapping; if (mapping.getMappedBy() != null) { Class<?> referenceClass = mapping.getReferenceClass(); ClassDescriptor referenceClassDescriptor = getClassDescriptor(referenceClass); return referenceClassDescriptor.getMappingForAttributeName(mapping.getMappedBy()); if (referenceMapping instanceof OneToManyMapping) { OneToManyMapping oneToManyMapping = (OneToManyMapping)referenceMapping; if (mapping.getAttributeName().equals(oneToManyMapping.getMappedBy())) { return oneToManyMapping;
if (SoftDelete.class.isAssignableFrom(oneToManyMapping.getReferenceClass())) { oneToManyMapping.setAdditionalJoinCriteria(new ExpressionBuilder().get("deleteTs").isNull());
/** * INTERNAL: * This method is used to create a change record from comparing two collections * @return org.eclipse.persistence.internal.sessions.ChangeRecord */ @Override public ChangeRecord compareForChange(Object clone, Object backUp, ObjectChangeSet owner, AbstractSession uow) { ChangeRecord record = super.compareForChange(clone, backUp, owner, uow); if(record != null && getReferenceDescriptor().getOptimisticLockingPolicy() != null) { postCalculateChanges(record, (UnitOfWorkImpl)uow); } return record; }
/** * PUBLIC: * Define the target foreign key relationship in the one-to-many mapping. * This method is used for composite target foreign key relationships. * That is, the target object's table has multiple foreign key fields to * the source object's (typically primary) key fields. * Both the target foreign key field names and the corresponding source primary * key field names must be specified. */ public void setTargetForeignKeyFieldNames(String[] targetForeignKeyFieldNames, String[] sourceKeyFieldNames) { if (targetForeignKeyFieldNames.length != sourceKeyFieldNames.length) { throw DescriptorException.targetForeignKeysSizeMismatch(this); } for (int i = 0; i < targetForeignKeyFieldNames.length; i++) { addTargetForeignKeyFieldName(targetForeignKeyFieldNames[i], sourceKeyFieldNames[i]); } }