Search in sources :

Example 1 with RelationTableMechanism

use of org.eclipse.persistence.mappings.RelationTableMechanism in project eclipselink by eclipse-ee4j.

the class ObjectAccessor method processOwningMappingKeys.

/**
 * INTERNAL:
 * Process the the correct metadata for the owning side of a one to one
 * mapping. Note, the order of checking is important, that is, check for
 * a mapsId first.
 */
protected void processOwningMappingKeys(OneToOneMapping mapping) {
    if (derivesId()) {
        // We need to process the join columns as we normally would.
        // Then we must update the fields from our derived id accessor.
        processForeignKeyRelationship(mapping);
        if (hasMapsId()) {
            processMapsId(mapping);
        } else {
            processId(mapping);
        }
    } else if (isOneToOnePrimaryKeyRelationship()) {
        processOneToOnePrimaryKeyRelationship(mapping);
    } else if (hasJoinTable()) {
        mapping.setRelationTableMechanism(new RelationTableMechanism());
        processJoinTable(mapping, mapping.getRelationTableMechanism(), getJoinTableMetadata());
    } else {
        processForeignKeyRelationship(mapping);
    }
}
Also used : RelationTableMechanism(org.eclipse.persistence.mappings.RelationTableMechanism)

Example 2 with RelationTableMechanism

use of org.eclipse.persistence.mappings.RelationTableMechanism in project eclipselink by eclipse-ee4j.

the class ExpressionQueryMechanism method buildDeleteAllStatementsForMappings.

// Create SQLDeleteAllStatements for mappings that may be responsible for references
// to the objects to be deleted
// in the tables NOT mapped to any class: ManyToManyMapping and DirectCollectionMapping
/**
 * NOTE: A similar pattern also used in method buildDeleteAllStatementsForMappingsWithTempTable:
 *  if you are updating this method consider applying a similar update to that method as well.
 *
 * @return {@code Vector<SQLDeleteAllStatement>}
 */
protected Vector buildDeleteAllStatementsForMappings(SQLCall selectCallForExist, SQLSelectStatement selectStatementForExist, boolean dontCheckDescriptor) {
    Vector deleteStatements = new Vector();
    ClassDescriptor descriptor = getDescriptor();
    for (DatabaseMapping mapping : descriptor.getMappings()) {
        if (mapping.isForeignReferenceMapping()) {
            Vector sourceFields = null;
            Vector targetFields = null;
            if (mapping.isDirectCollectionMapping()) {
                if (shouldBuildDeleteStatementForMapping((DirectCollectionMapping) mapping, dontCheckDescriptor, descriptor)) {
                    sourceFields = ((DirectCollectionMapping) mapping).getSourceKeyFields();
                    targetFields = ((DirectCollectionMapping) mapping).getReferenceKeyFields();
                }
            } else if (mapping.isAggregateCollectionMapping()) {
                if (shouldBuildDeleteStatementForMapping((AggregateCollectionMapping) mapping, dontCheckDescriptor, descriptor)) {
                    sourceFields = ((AggregateCollectionMapping) mapping).getSourceKeyFields();
                    targetFields = ((AggregateCollectionMapping) mapping).getTargetForeignKeyFields();
                }
            } else if (mapping.isManyToManyMapping()) {
                if (shouldBuildDeleteStatementForMapping((ManyToManyMapping) mapping, dontCheckDescriptor, descriptor)) {
                    RelationTableMechanism relationTableMechanism = ((ManyToManyMapping) mapping).getRelationTableMechanism();
                    sourceFields = relationTableMechanism.getSourceKeyFields();
                    targetFields = relationTableMechanism.getSourceRelationKeyFields();
                }
            } else if (mapping.isOneToOneMapping()) {
                RelationTableMechanism relationTableMechanism = ((OneToOneMapping) mapping).getRelationTableMechanism();
                if (relationTableMechanism != null) {
                    if (shouldBuildDeleteStatementForMapping((OneToOneMapping) mapping, dontCheckDescriptor, descriptor)) {
                        sourceFields = relationTableMechanism.getSourceKeyFields();
                        targetFields = relationTableMechanism.getSourceRelationKeyFields();
                    }
                }
            }
            if (sourceFields != null) {
                deleteStatements.add(buildDeleteAllStatementForMapping(selectCallForExist, selectStatementForExist, sourceFields, targetFields));
            }
        }
    }
    return deleteStatements;
}
Also used : ManyToManyMapping(org.eclipse.persistence.mappings.ManyToManyMapping) AggregateCollectionMapping(org.eclipse.persistence.mappings.AggregateCollectionMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) RelationTableMechanism(org.eclipse.persistence.mappings.RelationTableMechanism) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping)

Example 3 with RelationTableMechanism

use of org.eclipse.persistence.mappings.RelationTableMechanism in project eclipselink by eclipse-ee4j.

the class ExpressionQueryMechanism method buildDeleteAllStatementsForMappingsWithTempTable.

/**
 * Build delete statements with temporary table for ManyToMany and DirectCollection mappings.
 *
 * NOTE: A similar pattern also used in method buildDeleteAllStatementsForMappings():
 *  if you are updating this method consider applying a similar update to that method as well.
 *
 * @return {@code Vector<SQLDeleteAllStatementForTempTable>}
 */
protected Vector buildDeleteAllStatementsForMappingsWithTempTable(ClassDescriptor descriptor, DatabaseTable rootTable, boolean dontCheckDescriptor) {
    Vector deleteStatements = new Vector();
    for (DatabaseMapping mapping : descriptor.getMappings()) {
        if (mapping.isForeignReferenceMapping()) {
            List<DatabaseField> sourceFields = null;
            List<DatabaseField> targetFields = null;
            if (mapping.isDirectCollectionMapping()) {
                if (shouldBuildDeleteStatementForMapping((DirectCollectionMapping) mapping, dontCheckDescriptor, descriptor)) {
                    sourceFields = ((DirectCollectionMapping) mapping).getSourceKeyFields();
                    targetFields = ((DirectCollectionMapping) mapping).getReferenceKeyFields();
                }
            } else if (mapping.isAggregateCollectionMapping()) {
                if (shouldBuildDeleteStatementForMapping((AggregateCollectionMapping) mapping, dontCheckDescriptor, descriptor)) {
                    sourceFields = ((AggregateCollectionMapping) mapping).getSourceKeyFields();
                    targetFields = ((AggregateCollectionMapping) mapping).getTargetForeignKeyFields();
                }
            } else if (mapping.isManyToManyMapping()) {
                if (shouldBuildDeleteStatementForMapping((ManyToManyMapping) mapping, dontCheckDescriptor, descriptor)) {
                    RelationTableMechanism relationTableMechanism = ((ManyToManyMapping) mapping).getRelationTableMechanism();
                    sourceFields = relationTableMechanism.getSourceKeyFields();
                    targetFields = relationTableMechanism.getSourceRelationKeyFields();
                }
            } else if (mapping.isOneToOneMapping()) {
                RelationTableMechanism relationTableMechanism = ((OneToOneMapping) mapping).getRelationTableMechanism();
                if (relationTableMechanism != null) {
                    if (shouldBuildDeleteStatementForMapping((OneToOneMapping) mapping, dontCheckDescriptor, descriptor)) {
                        sourceFields = relationTableMechanism.getSourceKeyFields();
                        targetFields = relationTableMechanism.getSourceRelationKeyFields();
                    }
                }
            }
            if (sourceFields != null) {
                DatabaseTable targetTable = targetFields.get(0).getTable();
                SQLDeleteAllStatementForTempTable deleteStatement = buildDeleteAllStatementForTempTable(rootTable, sourceFields, targetTable, targetFields);
                deleteStatements.addElement(deleteStatement);
            }
        }
    }
    return deleteStatements;
}
Also used : ManyToManyMapping(org.eclipse.persistence.mappings.ManyToManyMapping) AggregateCollectionMapping(org.eclipse.persistence.mappings.AggregateCollectionMapping) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) RelationTableMechanism(org.eclipse.persistence.mappings.RelationTableMechanism) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping) SQLDeleteAllStatementForTempTable(org.eclipse.persistence.internal.expressions.SQLDeleteAllStatementForTempTable)

Example 4 with RelationTableMechanism

use of org.eclipse.persistence.mappings.RelationTableMechanism in project eclipselink by eclipse-ee4j.

the class DefaultTableGenerator method postInitTableSchema.

/**
 * Build additional table/field definitions for the descriptor, like relation table
 * and direct-collection, direct-map table, as well as reset LOB type for serialized
 * object mapping and type conversion mapping for LOB usage
 */
protected void postInitTableSchema(ClassDescriptor descriptor) {
    for (DatabaseMapping mapping : descriptor.getMappings()) {
        if (descriptor.isChildDescriptor() && descriptor.getInheritancePolicy().getParentDescriptor().getMappingForAttributeName(mapping.getAttributeName()) != null) {
            // times for the same table.
            continue;
        } else if (mapping.isManyToManyMapping()) {
            buildRelationTableDefinition((ManyToManyMapping) mapping, ((ManyToManyMapping) mapping).getRelationTableMechanism(), ((ManyToManyMapping) mapping).getListOrderField(), mapping.getContainerPolicy());
        } else if (mapping.isDirectCollectionMapping()) {
            buildDirectCollectionTableDefinition((DirectCollectionMapping) mapping, descriptor);
        } else if (mapping.isDirectToFieldMapping()) {
            Converter converter = ((DirectToFieldMapping) mapping).getConverter();
            if (converter != null) {
                if (converter instanceof TypeConversionConverter) {
                    resetFieldTypeForLOB((DirectToFieldMapping) mapping);
                }
                if (converter instanceof SerializedObjectConverter) {
                    // serialized object mapping field should be BLOB/IMAGE
                    getFieldDefFromDBField(mapping.getField()).setType(((SerializedObjectConverter) converter).getSerializer().getType());
                }
            }
        } else if (mapping.isAggregateCollectionMapping()) {
            // need to figure out the target foreign key field and add it into the aggregate target table
            createAggregateTargetTable((AggregateCollectionMapping) mapping);
        } else if (mapping.isForeignReferenceMapping()) {
            if (mapping.isOneToOneMapping()) {
                RelationTableMechanism relationTableMechanism = ((OneToOneMapping) mapping).getRelationTableMechanism();
                if (relationTableMechanism == null) {
                    addForeignKeyFieldToSourceTargetTable((OneToOneMapping) mapping);
                } else {
                    buildRelationTableDefinition((OneToOneMapping) mapping, relationTableMechanism, null, null);
                }
            } else if (mapping.isOneToManyMapping()) {
                addForeignKeyFieldToSourceTargetTable((OneToManyMapping) mapping);
                TableDefinition targTblDef = getTableDefFromDBTable(mapping.getReferenceDescriptor().getDefaultTable());
                addFieldsForMappedKeyMapContainerPolicy(mapping.getContainerPolicy(), targTblDef);
            }
        } else if (mapping.isTransformationMapping()) {
            resetTransformedFieldType((TransformationMapping) mapping);
        } else if (mapping.isAggregateObjectMapping()) {
            postInitTableSchema(mapping.getReferenceDescriptor());
        }
    }
    processAdditionalTablePkFields(descriptor);
}
Also used : ManyToManyMapping(org.eclipse.persistence.mappings.ManyToManyMapping) DirectToFieldMapping(org.eclipse.persistence.mappings.DirectToFieldMapping) TypeConversionConverter(org.eclipse.persistence.mappings.converters.TypeConversionConverter) SerializedObjectConverter(org.eclipse.persistence.mappings.converters.SerializedObjectConverter) Converter(org.eclipse.persistence.mappings.converters.Converter) TypeConversionConverter(org.eclipse.persistence.mappings.converters.TypeConversionConverter) SerializedObjectConverter(org.eclipse.persistence.mappings.converters.SerializedObjectConverter) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) RelationTableMechanism(org.eclipse.persistence.mappings.RelationTableMechanism) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping)

Example 5 with RelationTableMechanism

use of org.eclipse.persistence.mappings.RelationTableMechanism in project eclipselink by eclipse-ee4j.

the class OneToOneAccessor method process.

/**
 * INTERNAL:
 * Process a one to one setting into an EclipseLink OneToOneMapping.
 */
@Override
public void process() {
    super.process();
    // Initialize our mapping now with what we found.
    ObjectReferenceMapping mapping = initOneToOneMapping();
    if (hasMappedBy()) {
        // Non-owning side, process the foreign keys from the owner.
        DatabaseMapping owningMapping = getOwningMapping();
        if (owningMapping.isOneToOneMapping()) {
            OneToOneMapping ownerMapping = (OneToOneMapping) owningMapping;
            // as we would for a many-to-many mapping.
            if (ownerMapping.hasRelationTableMechanism()) {
                // Put a relation table mechanism on our mapping.
                ((OneToOneMapping) mapping).setRelationTableMechanism(new RelationTableMechanism());
                processMappedByRelationTable(ownerMapping.getRelationTableMechanism(), ((OneToOneMapping) mapping).getRelationTableMechanism());
            } else {
                Map<DatabaseField, DatabaseField> targetToSourceKeyFields;
                Map<DatabaseField, DatabaseField> sourceToTargetKeyFields;
                // the primary key field to point to our own database table.
                if (getDescriptor().usesTablePerClassInheritanceStrategy()) {
                    targetToSourceKeyFields = new HashMap<DatabaseField, DatabaseField>();
                    sourceToTargetKeyFields = new HashMap<DatabaseField, DatabaseField>();
                    for (DatabaseField fkField : ownerMapping.getSourceToTargetKeyFields().keySet()) {
                        // We need to update the pk field to be to our table.
                        DatabaseField pkField = ownerMapping.getSourceToTargetKeyFields().get(fkField).clone();
                        pkField.setTable(getDescriptor().getPrimaryTable());
                        sourceToTargetKeyFields.put(fkField, pkField);
                        targetToSourceKeyFields.put(pkField, fkField);
                    }
                } else {
                    targetToSourceKeyFields = ownerMapping.getTargetToSourceKeyFields();
                    sourceToTargetKeyFields = ownerMapping.getSourceToTargetKeyFields();
                }
                ((OneToOneMapping) mapping).setSourceToTargetKeyFields(targetToSourceKeyFields);
                ((OneToOneMapping) mapping).setTargetToSourceKeyFields(sourceToTargetKeyFields);
            }
        } else {
            // If improper mapping encountered, throw an exception.
            throw ValidationException.invalidMapping(getJavaClass(), getReferenceClass());
        }
    } else if (mapping instanceof OneToOneMapping) {
        // Owning side, look for JoinColumns or PrimaryKeyJoinColumns.
        processOwningMappingKeys((OneToOneMapping) mapping);
    } else {
        processForeignKeyRelationship(mapping);
    }
}
Also used : ObjectReferenceMapping(org.eclipse.persistence.mappings.ObjectReferenceMapping) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping) RelationTableMechanism(org.eclipse.persistence.mappings.RelationTableMechanism)

Aggregations

RelationTableMechanism (org.eclipse.persistence.mappings.RelationTableMechanism)5 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)4 OneToOneMapping (org.eclipse.persistence.mappings.OneToOneMapping)4 ManyToManyMapping (org.eclipse.persistence.mappings.ManyToManyMapping)3 Vector (java.util.Vector)2 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)2 NonSynchronizedVector (org.eclipse.persistence.internal.helper.NonSynchronizedVector)2 AggregateCollectionMapping (org.eclipse.persistence.mappings.AggregateCollectionMapping)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 SQLDeleteAllStatementForTempTable (org.eclipse.persistence.internal.expressions.SQLDeleteAllStatementForTempTable)1 DatabaseTable (org.eclipse.persistence.internal.helper.DatabaseTable)1 DirectToFieldMapping (org.eclipse.persistence.mappings.DirectToFieldMapping)1 ObjectReferenceMapping (org.eclipse.persistence.mappings.ObjectReferenceMapping)1 Converter (org.eclipse.persistence.mappings.converters.Converter)1 SerializedObjectConverter (org.eclipse.persistence.mappings.converters.SerializedObjectConverter)1 TypeConversionConverter (org.eclipse.persistence.mappings.converters.TypeConversionConverter)1