Search in sources :

Example 1 with PrimaryKeyJoinColumnMetadata

use of org.eclipse.persistence.internal.jpa.metadata.columns.PrimaryKeyJoinColumnMetadata in project eclipselink by eclipse-ee4j.

the class ObjectAccessor method processOneToOnePrimaryKeyRelationship.

/**
 * INTERNAL:
 * Process the primary key join columns for the owning side of a one to one
 * mapping. The default pk and pk field names are used only with single
 * primary key entities. The processor should never get as far as to use
 * them with entities that have a composite primary key (validation
 * exception will be thrown).
 */
protected void processOneToOnePrimaryKeyRelationship(OneToOneMapping mapping) {
    List<PrimaryKeyJoinColumnMetadata> pkJoinColumns = processPrimaryKeyJoinColumns(getPrimaryKeyJoinColumns());
    // Add the source foreign key fields to the mapping.
    for (PrimaryKeyJoinColumnMetadata primaryKeyJoinColumn : pkJoinColumns) {
        // Look up the primary key field from the referenced column name.
        DatabaseField pkField = getReferencedField(primaryKeyJoinColumn.getReferencedColumnName(), getReferenceDescriptor(), MetadataLogger.PK_COLUMN);
        // Look up the foreign key field from the name.
        DatabaseField fkField = getReferencedField(primaryKeyJoinColumn.getName(), getDescriptor(), MetadataLogger.FK_COLUMN);
        // Add a source foreign key to the mapping.
        mapping.addForeignKeyField(fkField, pkField);
    }
    // Mark the mapping read only
    mapping.setIsReadOnly(true);
    // Set the primary key join column flag. This will also set the mapping
    // to read only.
    mapping.setIsOneToOnePrimaryKeyRelationship(true);
    // accessor.
    if (m_primaryKeyForeignKey != null) {
        m_primaryKeyForeignKey.process(getDescriptor().getPrimaryKeyTable());
    }
}
Also used : PrimaryKeyJoinColumnMetadata(org.eclipse.persistence.internal.jpa.metadata.columns.PrimaryKeyJoinColumnMetadata) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField)

Example 2 with PrimaryKeyJoinColumnMetadata

use of org.eclipse.persistence.internal.jpa.metadata.columns.PrimaryKeyJoinColumnMetadata in project eclipselink by eclipse-ee4j.

the class BasicCollectionAccessor method processCollectionTable.

/**
 * INTERNAL:
 * Process a MetadataCollectionTable.
 */
@Override
protected void processCollectionTable(CollectionMapping mapping) {
    super.processCollectionTable(mapping);
    // processPrimaryKeyJoinColumns call.
    for (PrimaryKeyJoinColumnMetadata primaryKeyJoinColumn : processPrimaryKeyJoinColumns(getCollectionTable().getPrimaryKeyJoinColumns())) {
        // Look up the primary key field from the referenced column name.
        DatabaseField pkField = getReferencedField(primaryKeyJoinColumn.getReferencedColumnName(), getOwningDescriptor(), MetadataLogger.PK_COLUMN);
        // The default name is the primary key of the owning entity.
        DatabaseField fkField = primaryKeyJoinColumn.getForeignKeyField(pkField);
        setFieldName(fkField, getOwningDescriptor().getPrimaryKeyFieldName(), MetadataLogger.FK_COLUMN);
        fkField.setTable(getReferenceDatabaseTable());
        // Add the reference key field for the direct collection mapping.
        ((DirectCollectionMapping) mapping).addReferenceKeyField(fkField, pkField);
    }
}
Also used : PrimaryKeyJoinColumnMetadata(org.eclipse.persistence.internal.jpa.metadata.columns.PrimaryKeyJoinColumnMetadata) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DirectCollectionMapping(org.eclipse.persistence.mappings.DirectCollectionMapping)

Example 3 with PrimaryKeyJoinColumnMetadata

use of org.eclipse.persistence.internal.jpa.metadata.columns.PrimaryKeyJoinColumnMetadata in project eclipselink by eclipse-ee4j.

the class MetadataAccessor method processPrimaryKeyJoinColumns.

/**
 * INTERNAL:
 * Process the primary key join columms for this accessors annotated element.
 */
protected List<PrimaryKeyJoinColumnMetadata> processPrimaryKeyJoinColumns(List<PrimaryKeyJoinColumnMetadata> primaryKeyJoinColumns) {
    // this call will add any defaulted columns as necessary.
    if (primaryKeyJoinColumns.isEmpty()) {
        if (getDescriptor().hasCompositePrimaryKey()) {
            // key. Foreign and primary key to have the same name.
            for (DatabaseField primaryKeyField : getDescriptor().getPrimaryKeyFields()) {
                PrimaryKeyJoinColumnMetadata primaryKeyJoinColumn = new PrimaryKeyJoinColumnMetadata(getProject());
                primaryKeyJoinColumn.setReferencedColumnName(primaryKeyField.getName());
                primaryKeyJoinColumn.setName(primaryKeyField.getName());
                primaryKeyJoinColumns.add(primaryKeyJoinColumn);
            }
        } else {
            // Add a default one for the single case, not setting any
            // foreign and primary key names. They will default based
            // on which accessor is using them.
            primaryKeyJoinColumns.add(new PrimaryKeyJoinColumnMetadata(getProject()));
        }
    } else {
        // add the multitenant primary key tenant discriminator fields.
        if (getDescriptor().hasSingleTableMultitenant() && primaryKeyJoinColumns.size() != getDescriptor().getPrimaryKeyFields().size()) {
            SingleTableMultitenantPolicy policy = (SingleTableMultitenantPolicy) getDescriptor().getClassDescriptor().getMultitenantPolicy();
            Map<DatabaseField, String> tenantFields = policy.getTenantDiscriminatorFields();
            // Go through the key sets ...
            for (DatabaseField tenantField : tenantFields.keySet()) {
                if (tenantField.isPrimaryKey()) {
                    PrimaryKeyJoinColumnMetadata primaryKeyJoinColumn = new PrimaryKeyJoinColumnMetadata();
                    primaryKeyJoinColumn.setName(tenantField.getName());
                    primaryKeyJoinColumn.setReferencedColumnName(tenantField.getName());
                    primaryKeyJoinColumn.setProject(getProject());
                    primaryKeyJoinColumns.add(primaryKeyJoinColumn);
                }
            }
        }
    }
    // Now validate what we have.
    if (getDescriptor().hasCompositePrimaryKey()) {
        // key columns since we allow joining to partials.
        for (PrimaryKeyJoinColumnMetadata pkJoinColumn : primaryKeyJoinColumns) {
            if (pkJoinColumn.isPrimaryKeyFieldNotSpecified() || pkJoinColumn.isForeignKeyFieldNotSpecified()) {
                throw ValidationException.incompletePrimaryKeyJoinColumnsSpecified(getAnnotatedElement());
            }
        }
    } else {
        if (primaryKeyJoinColumns.size() > 1) {
            throw ValidationException.excessivePrimaryKeyJoinColumnsSpecified(getAnnotatedElement());
        }
    }
    return primaryKeyJoinColumns;
}
Also used : PrimaryKeyJoinColumnMetadata(org.eclipse.persistence.internal.jpa.metadata.columns.PrimaryKeyJoinColumnMetadata) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) SingleTableMultitenantPolicy(org.eclipse.persistence.descriptors.SingleTableMultitenantPolicy)

Example 4 with PrimaryKeyJoinColumnMetadata

use of org.eclipse.persistence.internal.jpa.metadata.columns.PrimaryKeyJoinColumnMetadata in project eclipselink by eclipse-ee4j.

the class EntityAccessor method addMultipleTableKeyFields.

/**
 * INTERNAL:
 * Add multiple fields to the descriptor. Called from either Inheritance
 * or SecondaryTable context.
 */
protected void addMultipleTableKeyFields(List<PrimaryKeyJoinColumnMetadata> primaryKeyJoinColumns, DatabaseTable targetTable, String PK_CTX, String FK_CTX) {
    // columns passed in and will return a list of PrimaryKeyJoinColumnMetadata.
    for (PrimaryKeyJoinColumnMetadata primaryKeyJoinColumn : processPrimaryKeyJoinColumns(primaryKeyJoinColumns)) {
        // Look up the primary key field from the referenced column name.
        DatabaseField pkField = getReferencedField(primaryKeyJoinColumn.getReferencedColumnName(), getDescriptor(), PK_CTX);
        DatabaseField fkField = primaryKeyJoinColumn.getForeignKeyField(pkField);
        setFieldName(fkField, pkField.getName(), FK_CTX);
        fkField.setTable(targetTable);
        // Add the foreign key field to the descriptor.
        getDescriptor().addForeignKeyFieldForMultipleTable(fkField, pkField);
    }
}
Also used : PrimaryKeyJoinColumnMetadata(org.eclipse.persistence.internal.jpa.metadata.columns.PrimaryKeyJoinColumnMetadata) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField)

Example 5 with PrimaryKeyJoinColumnMetadata

use of org.eclipse.persistence.internal.jpa.metadata.columns.PrimaryKeyJoinColumnMetadata in project eclipselink by eclipse-ee4j.

the class EntityAccessor method processInheritancePrimaryKeyJoinColumns.

/**
 * INTERNAL:
 * Process the inheritance metadata for an inheritance subclass. The
 * parent descriptor must be provided.
 */
public void processInheritancePrimaryKeyJoinColumns() {
    // some defined through annotations.
    if (m_primaryKeyJoinColumns.isEmpty()) {
        // Process all the primary key join columns first.
        if (isAnnotationPresent(JPA_PRIMARY_KEY_JOIN_COLUMNS)) {
            MetadataAnnotation primaryKeyJoinColumns = getAnnotation(JPA_PRIMARY_KEY_JOIN_COLUMNS);
            for (Object primaryKeyJoinColumn : primaryKeyJoinColumns.getAttributeArray("value")) {
                m_primaryKeyJoinColumns.add(new PrimaryKeyJoinColumnMetadata((MetadataAnnotation) primaryKeyJoinColumn, this));
            }
            // Set the primary key foreign key metadata if one is specified.
            if (primaryKeyJoinColumns.hasAttribute("foreignKey")) {
                setPrimaryKeyForeignKey(new PrimaryKeyForeignKeyMetadata(primaryKeyJoinColumns.getAttributeAnnotation("foreignKey"), this));
            }
        }
        // Process the single primary key join column second.
        if (isAnnotationPresent(JPA_PRIMARY_KEY_JOIN_COLUMN)) {
            PrimaryKeyJoinColumnMetadata primaryKeyJoinColumn = new PrimaryKeyJoinColumnMetadata(getAnnotation(JPA_PRIMARY_KEY_JOIN_COLUMN), this);
            m_primaryKeyJoinColumns.add(primaryKeyJoinColumn);
            // Set the primary key foreign key metadata if specified.
            if (primaryKeyJoinColumn.hasForeignKey()) {
                setPrimaryKeyForeignKey(new PrimaryKeyForeignKeyMetadata(primaryKeyJoinColumn.getForeignKey()));
            }
        }
    }
    // Process the primary key join columns into multiple table key fields.
    addMultipleTableKeyFields(m_primaryKeyJoinColumns, getDescriptor().getPrimaryTable(), MetadataLogger.INHERITANCE_PK_COLUMN, MetadataLogger.INHERITANCE_FK_COLUMN);
    // Process the primary key foreign key.
    if (m_primaryKeyForeignKey != null) {
        m_primaryKeyForeignKey.process(getDescriptor().getPrimaryTable());
    }
}
Also used : PrimaryKeyForeignKeyMetadata(org.eclipse.persistence.internal.jpa.metadata.columns.PrimaryKeyForeignKeyMetadata) PrimaryKeyJoinColumnMetadata(org.eclipse.persistence.internal.jpa.metadata.columns.PrimaryKeyJoinColumnMetadata) MetadataAccessibleObject(org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataAccessibleObject) MetadataAnnotation(org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataAnnotation)

Aggregations

PrimaryKeyJoinColumnMetadata (org.eclipse.persistence.internal.jpa.metadata.columns.PrimaryKeyJoinColumnMetadata)5 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)4 SingleTableMultitenantPolicy (org.eclipse.persistence.descriptors.SingleTableMultitenantPolicy)1 MetadataAccessibleObject (org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataAccessibleObject)1 MetadataAnnotation (org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataAnnotation)1 PrimaryKeyForeignKeyMetadata (org.eclipse.persistence.internal.jpa.metadata.columns.PrimaryKeyForeignKeyMetadata)1 DirectCollectionMapping (org.eclipse.persistence.mappings.DirectCollectionMapping)1