Search in sources :

Example 1 with OneToOneMapping

use of org.eclipse.persistence.mappings.OneToOneMapping in project cuba by cuba-platform.

the class JoinCriteriaMappingProcessor method process.

@Override
public void process(MappingProcessorContext context) {
    DatabaseMapping mapping = context.getMapping();
    Expression expression = AppBeans.getAll(JoinExpressionProvider.class).values().stream().map(provider -> provider.getJoinCriteriaExpression(mapping)).filter(Objects::nonNull).reduce(Expression::and).orElse(null);
    // Applying additional join criteria, e.g. for soft delete or multitenancy -> move to mapping processor
    if (mapping.isOneToManyMapping() || mapping.isOneToOneMapping()) {
        // Apply expression to mappings
        if (mapping.isOneToManyMapping()) {
            ((OneToManyMapping) mapping).setAdditionalJoinCriteria(expression);
        } else if (mapping.isOneToOneMapping()) {
            ((OneToOneMapping) mapping).setAdditionalJoinCriteria(expression);
        }
    }
}
Also used : Objects(java.util.Objects) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping) Component(org.springframework.stereotype.Component) Expression(org.eclipse.persistence.expressions.Expression) OneToManyMapping(org.eclipse.persistence.mappings.OneToManyMapping) AppBeans(com.haulmont.cuba.core.global.AppBeans) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) Expression(org.eclipse.persistence.expressions.Expression) OneToManyMapping(org.eclipse.persistence.mappings.OneToManyMapping) Objects(java.util.Objects) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping)

Example 2 with OneToOneMapping

use of org.eclipse.persistence.mappings.OneToOneMapping in project jmix by jmix-framework.

the class SoftDeleteProcessor method process.

@Override
public void process(MappingProcessorContext context) {
    DatabaseMapping mapping = context.getMapping();
    MetaClass metaClass = metadata.getClass(mapping.getDescriptor().getJavaClass());
    MetaProperty metaProperty = metaClass.getProperty(mapping.getAttributeName());
    if (mapping.isOneToOneMapping()) {
        OneToOneMapping oneToOneMapping = (OneToOneMapping) mapping;
        if (metadataTools.isSoftDeletable(oneToOneMapping.getReferenceClass())) {
            if (mapping.isManyToOneMapping()) {
                oneToOneMapping.setSoftDeletionForBatch(false);
                oneToOneMapping.setSoftDeletionForValueHolder(false);
            } else {
                OneToOne oneToOne = metaProperty.getAnnotatedElement().getAnnotation(OneToOne.class);
                if (oneToOne != null) {
                    if (Strings.isNullOrEmpty(oneToOne.mappedBy())) {
                        oneToOneMapping.setSoftDeletionForBatch(false);
                        oneToOneMapping.setSoftDeletionForValueHolder(false);
                    }
                }
            }
        }
    }
}
Also used : OneToOne(javax.persistence.OneToOne) MetaClass(io.jmix.core.metamodel.model.MetaClass) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping)

Example 3 with OneToOneMapping

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

the class MappingAccessor method processEntityMapKeyClass.

/**
 * INTERNAL:
 * Process the map key to be an entity class.
 */
protected OneToOneMapping processEntityMapKeyClass(MappedKeyMapAccessor mappedKeyMapAccessor) {
    String mapKeyClassName = mappedKeyMapAccessor.getMapKeyClass().getName();
    // Create the one to one map key mapping.
    OneToOneMapping keyMapping = new OneToOneMapping();
    keyMapping.setReferenceClassName(mapKeyClassName);
    keyMapping.dontUseIndirection();
    keyMapping.setDescriptor(getDescriptor().getClassDescriptor());
    // Process the map key join columns.
    EntityAccessor mapKeyAccessor = getProject().getEntityAccessor(mapKeyClassName);
    MetadataDescriptor mapKeyClassDescriptor = mapKeyAccessor.getDescriptor();
    // If the fk field (name) is not specified, it defaults to the
    // concatenation of the following: the name of the referencing
    // relationship property or field of the referencing entity or
    // embeddable; "_"; "KEY"
    String defaultFKFieldName = getAttributeName() + DEFAULT_MAP_KEY_COLUMN_SUFFIX;
    // Get the join columns (directly or through an association override),
    // init them and validate.
    List<JoinColumnMetadata> joinColumns = getJoinColumns(mappedKeyMapAccessor.getMapKeyJoinColumns(), mapKeyClassDescriptor);
    // Get the foreign key (directly or through an association override) and
    // make sure it is initialized for processing.
    ForeignKeyMetadata foreignKey = getForeignKey(mappedKeyMapAccessor.getMapKeyForeignKey(), mapKeyClassDescriptor);
    // Now process the foreign key relationship metadata.
    processForeignKeyRelationship(keyMapping, joinColumns, foreignKey, mapKeyClassDescriptor, defaultFKFieldName, getDefaultTableForEntityMapKey());
    return keyMapping;
}
Also used : EntityAccessor(org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor) ForeignKeyMetadata(org.eclipse.persistence.internal.jpa.metadata.columns.ForeignKeyMetadata) MetadataDescriptor(org.eclipse.persistence.internal.jpa.metadata.MetadataDescriptor) JoinColumnMetadata(org.eclipse.persistence.internal.jpa.metadata.columns.JoinColumnMetadata) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping)

Example 4 with OneToOneMapping

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

the class CMP3Policy method createPrimaryKeyFromId.

/**
 * INTERNAL:
 * Use the key to create a EclipseLink primary key.
 * If the key is simple (direct mapped) then just add it to a vector,
 * otherwise must go through the inefficient process of copying the key into the bean
 * and extracting the key from the bean.
 */
@Override
public Object createPrimaryKeyFromId(Object key, AbstractSession session) {
    // If the descriptor primary key is mapped through direct-to-field mappings,
    // then no elaborate conversion is required.
    // If key is compound, add each value to the vector.
    KeyElementAccessor[] pkElementArray = this.getKeyClassFields();
    Object[] primaryKey = null;
    if (getDescriptor().getCacheKeyType() != CacheKeyType.ID_VALUE) {
        primaryKey = new Object[pkElementArray.length];
    }
    for (int index = 0; index < pkElementArray.length; index++) {
        DatabaseMapping mapping = pkElementArray[index].getMapping();
        Object fieldValue = null;
        if (mapping.isAbstractColumnMapping()) {
            if (pkElementArray[index].isNestedAccessor()) {
                // We have nested aggregate(s) in the embedded id pkclass.
                DatabaseField keyField = pkElementArray[index].getDatabaseField();
                Object keyToUse = key;
                DatabaseMapping keyMapping = getDescriptor().getObjectBuilder().getMappingForField(keyField);
                if (keyMapping.isAggregateMapping()) {
                    keyMapping = keyMapping.getReferenceDescriptor().getObjectBuilder().getMappingForField(keyField);
                    // Keep driving down the nested aggregates ...
                    while (keyMapping.isAggregateMapping()) {
                        keyToUse = keyMapping.getRealAttributeValueFromObject(keyToUse, session);
                        keyMapping = keyMapping.getReferenceDescriptor().getObjectBuilder().getMappingForField(keyField);
                    }
                    fieldValue = ((AbstractColumnMapping) mapping).getFieldValue(pkElementArray[index].getValue(keyToUse, session), session);
                } else {
                    // This should never hit but just in case ... better to get a proper exception rather than a NPE etc.
                    fieldValue = ((AbstractColumnMapping) mapping).getFieldValue(pkElementArray[index].getValue(keyToUse, session), session);
                }
            } else {
                fieldValue = ((AbstractColumnMapping) mapping).getFieldValue(pkElementArray[index].getValue(key, session), session);
            }
        } else {
            fieldValue = pkElementArray[index].getValue(key, session);
            if ((fieldValue != null) && (pkClass != null) && (mapping.isOneToOneMapping())) {
                OneToOneMapping refmapping = (OneToOneMapping) mapping;
                DatabaseField targetKey = refmapping.getSourceToTargetKeyFields().get(pkElementArray[index].getDatabaseField());
                CMPPolicy refPolicy = refmapping.getReferenceDescriptor().getCMPPolicy();
                if (refPolicy.isCMP3Policy()) {
                    Class<Object> aPKClass = refPolicy.getPKClass();
                    if ((aPKClass != null) && (aPKClass != fieldValue.getClass()) && (!aPKClass.isAssignableFrom(fieldValue.getClass()))) {
                        throw new IllegalArgumentException(ExceptionLocalization.buildMessage("invalid_pk_class", new Object[] { aPKClass, fieldValue.getClass() }));
                    }
                    fieldValue = ((CMP3Policy) refPolicy).getPkValueFromKeyForField(fieldValue, targetKey, session);
                }
            }
        }
        if (getDescriptor().getCacheKeyType() == CacheKeyType.ID_VALUE) {
            return fieldValue;
        }
        primaryKey[index] = fieldValue;
    }
    return new CacheId(primaryKey);
}
Also used : CMPPolicy(org.eclipse.persistence.descriptors.CMPPolicy) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping)

Example 5 with OneToOneMapping

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

the class CMP3Policy method getPkValueFromKeyForField.

/**
 * INTERNAL:
 * Pull the value for the field from the key.
 *
 * @param key Object the primary key to use to get the value for the field
 * @param field DatabaseField the field to find a value for
 * @return Object
 */
public Object getPkValueFromKeyForField(Object key, DatabaseField field, AbstractSession session) {
    Object fieldValue = null;
    KeyElementAccessor accessor = this.fieldToAccessorMap.get(field);
    DatabaseMapping mapping = accessor.getMapping();
    if (mapping.isAbstractColumnMapping()) {
        fieldValue = ((AbstractColumnMapping) mapping).getFieldValue(accessor.getValue(key, session), session);
    } else {
        fieldValue = accessor.getValue(key, session);
        if (mapping.isOneToOneMapping()) {
            OneToOneMapping refmapping = (OneToOneMapping) mapping;
            DatabaseField targetKey = refmapping.getSourceToTargetKeyFields().get(accessor.getDatabaseField());
            CMPPolicy refPolicy = refmapping.getReferenceDescriptor().getCMPPolicy();
            if (refPolicy.isCMP3Policy()) {
                Class<Object> pkClass = refPolicy.getPKClass();
                if ((pkClass != null) && (pkClass != fieldValue.getClass()) && (!pkClass.isAssignableFrom(fieldValue.getClass()))) {
                    throw new IllegalArgumentException(ExceptionLocalization.buildMessage("invalid_pk_class", new Object[] { refPolicy.getPKClass(), fieldValue.getClass() }));
                }
                fieldValue = ((CMP3Policy) refPolicy).getPkValueFromKeyForField(fieldValue, targetKey, session);
            }
        }
    }
    return fieldValue;
}
Also used : CMPPolicy(org.eclipse.persistence.descriptors.CMPPolicy) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping)

Aggregations

OneToOneMapping (org.eclipse.persistence.mappings.OneToOneMapping)97 RelationalDescriptor (org.eclipse.persistence.descriptors.RelationalDescriptor)38 DirectToFieldMapping (org.eclipse.persistence.mappings.DirectToFieldMapping)32 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)27 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)24 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)17 DatabaseTable (org.eclipse.persistence.internal.helper.DatabaseTable)15 ManyToManyMapping (org.eclipse.persistence.mappings.ManyToManyMapping)15 OneToManyMapping (org.eclipse.persistence.mappings.OneToManyMapping)15 Vector (java.util.Vector)11 Expression (org.eclipse.persistence.expressions.Expression)8 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 DirectCollectionMapping (org.eclipse.persistence.mappings.DirectCollectionMapping)5 DirectMapMapping (org.eclipse.persistence.mappings.DirectMapMapping)5 SessionBroker (org.eclipse.persistence.sessions.broker.SessionBroker)5 Test (org.junit.Test)5 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)4 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)4 RelationTableMechanism (org.eclipse.persistence.mappings.RelationTableMechanism)4