Search in sources :

Example 6 with Association

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

the class ClassDescriptor method getMultipleTablePrimaryKeyAssociations.

/**
 * INTERNAL:
 * Returns the foreign key relationships used for multiple tables which were specified by the user. Used
 * by the Project XML writer to output these associations
 *
 * @see #adjustMultipleTableInsertOrder()
 */
public Vector getMultipleTablePrimaryKeyAssociations() {
    Vector associations = new Vector(getAdditionalTablePrimaryKeyFields().size() * 2);
    Iterator<Map<DatabaseField, DatabaseField>> tablesHashtable = getAdditionalTablePrimaryKeyFields().values().iterator();
    while (tablesHashtable.hasNext()) {
        Map<DatabaseField, DatabaseField> tableHash = tablesHashtable.next();
        Iterator<DatabaseField> fieldEnumeration = tableHash.keySet().iterator();
        while (fieldEnumeration.hasNext()) {
            DatabaseField keyField = fieldEnumeration.next();
            // PRS#36802(CR#2057) contains() is changed to containsKey()
            if (!getMultipleTableForeignKeys().containsKey(keyField.getTable())) {
                Association association = new Association(keyField.getQualifiedName(), tableHash.get(keyField).getQualifiedName());
                associations.addElement(association);
            }
        }
    }
    return associations;
}
Also used : Association(org.eclipse.persistence.mappings.Association) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) Map(java.util.Map) IdentityMap(org.eclipse.persistence.internal.identitymaps.IdentityMap) HashMap(java.util.HashMap)

Example 7 with Association

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

the class InheritancePolicy method setClassIndicatorAssociations.

/**
 * INTERNAL:
 * Set the class indicator associations from reading the deployment XML.
 */
public void setClassIndicatorAssociations(Vector classIndicatorAssociations) {
    setClassNameIndicatorMapping(new HashMap(classIndicatorAssociations.size() + 1));
    setClassIndicatorMapping(new HashMap((classIndicatorAssociations.size() * 2) + 1));
    for (Iterator iterator = classIndicatorAssociations.iterator(); iterator.hasNext(); ) {
        Association association = (Association) iterator.next();
        Object key = association.getKey();
        // Allow for 904 format which stored class name, may not use correct class loader.
        if (key instanceof String) {
            key = ConversionManager.getDefaultManager().convertClassNameToClass((String) key);
        }
        addClassIndicator((Class) key, association.getValue());
    }
}
Also used : Association(org.eclipse.persistence.mappings.Association) TypedAssociation(org.eclipse.persistence.mappings.TypedAssociation) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) Iterator(java.util.Iterator)

Example 8 with Association

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

the class MappedKeyMapContainerPolicy method createChangeSetForKeys.

/**
 * INTERNAL:
 * Create change sets that contain map keys.
 */
@Override
protected void createChangeSetForKeys(Map originalKeyValues, CollectionChangeRecord changeRecord, AbstractSession session, ClassDescriptor referenceDescriptor) {
    Iterator originalKeyValuesIterator = originalKeyValues.values().iterator();
    while (originalKeyValuesIterator.hasNext()) {
        Association association = (Association) originalKeyValuesIterator.next();
        Object object = association.getValue();
        ObjectChangeSet changeSet = referenceDescriptor.getObjectBuilder().createObjectChangeSet(object, (UnitOfWorkChangeSet) changeRecord.getOwner().getUOWChangeSet(), session);
        changeSet.setOldKey(association.getKey());
    }
}
Also used : Association(org.eclipse.persistence.mappings.Association) Iterator(java.util.Iterator) DescriptorIterator(org.eclipse.persistence.internal.descriptors.DescriptorIterator) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet)

Example 9 with Association

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

the class ReportQueryResult method processItem.

/**
 * INTERNAL:
 * Return a value from an item and database row (converted from raw field values using the mapping).
 */
protected Object processItem(ReportQuery query, AbstractRecord row, Vector toManyData, ReportItem item) {
    JoinedAttributeManager joinManager = null;
    if (item.hasJoining()) {
        joinManager = item.getJoinedAttributeManager();
        if (joinManager.isToManyJoin()) {
            // PERF: Only reset data-result if unset, must only occur once per item, not per row (n vs n^2).
            if (joinManager.getDataResults_() == null) {
                joinManager.setDataResults(new ArrayList(toManyData), query.getSession());
            }
        }
    }
    Object value = null;
    int rowSize = row.size();
    int itemIndex = item.getResultIndex();
    DatabaseMapping mapping = item.getMapping();
    ClassDescriptor descriptor = item.getDescriptor();
    if (item.getAttributeExpression() != null) {
        if (descriptor == null && mapping != null) {
            descriptor = mapping.getReferenceDescriptor();
        }
        if (mapping != null && (mapping.isAbstractColumnMapping() || mapping.isDirectCollectionMapping())) {
            if (itemIndex >= rowSize) {
                throw QueryException.reportQueryResultSizeMismatch(itemIndex + 1, rowSize);
            }
            value = processItemFromMapping(query, row, mapping, item, itemIndex);
            // GF_ISSUE_395+
            if (this.key != null) {
                this.key.append(value);
                this.key.append("_");
            }
        } else if (descriptor != null) {
            // Item is for an object result.
            int size = descriptor.getAllSelectionFields(query).size();
            if (itemIndex + size > rowSize) {
                throw QueryException.reportQueryResultSizeMismatch(itemIndex + size, rowSize);
            }
            AbstractRecord subRow = row;
            // Check if at the start of the row, then avoid building a subRow.
            if (itemIndex > 0) {
                Vector<DatabaseField> trimedFields = new NonSynchronizedSubVector<>(row.getFields(), itemIndex, rowSize);
                Vector trimedValues = new NonSynchronizedSubVector(row.getValues(), itemIndex, rowSize);
                subRow = new DatabaseRecord(trimedFields, trimedValues);
            }
            if (mapping != null && mapping.isAggregateObjectMapping()) {
                value = ((AggregateObjectMapping) mapping).buildAggregateFromRow(subRow, null, null, joinManager, query, false, query.getSession(), true);
            } else {
                // TODO : Support prefrechedCacheKeys in report query
                value = descriptor.getObjectBuilder().buildObject(query, subRow, joinManager);
            }
            // a specific mapping.  This could happen in a MapContainerPolicy
            if (item.getAttributeExpression().isMapEntryExpression() && mapping.isCollectionMapping()) {
                Object rowKey = null;
                if (mapping.getContainerPolicy().isMapPolicy() && !mapping.getContainerPolicy().isMappedKeyMapPolicy()) {
                    rowKey = mapping.getContainerPolicy().keyFrom(value, query.getSession());
                } else {
                    rowKey = mapping.getContainerPolicy().buildKey(subRow, query, null, query.getSession(), true);
                }
                if (((MapEntryExpression) item.getAttributeExpression()).shouldReturnMapEntry()) {
                    value = new Association(rowKey, value);
                } else {
                    value = rowKey;
                }
            }
            // GF_ISSUE_395
            if (this.key != null) {
                Object primaryKey = descriptor.getObjectBuilder().extractPrimaryKeyFromRow(subRow, query.getSession());
                if (primaryKey != null) {
                    // GF3233 NPE is caused by processing the null PK being extracted from referenced target with null values in database.
                    this.key.append(primaryKey);
                }
                this.key.append("_");
            }
        } else {
            value = row.getValues().get(itemIndex);
            // GF_ISSUE_395
            if (this.key != null) {
                this.key.append(value);
            }
        }
    }
    return value;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) AggregateObjectMapping(org.eclipse.persistence.mappings.AggregateObjectMapping) JoinedAttributeManager(org.eclipse.persistence.internal.queries.JoinedAttributeManager) ArrayList(java.util.ArrayList) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) Association(org.eclipse.persistence.mappings.Association) NonSynchronizedSubVector(org.eclipse.persistence.internal.helper.NonSynchronizedSubVector) Vector(java.util.Vector) NonSynchronizedSubVector(org.eclipse.persistence.internal.helper.NonSynchronizedSubVector)

Example 10 with Association

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

the class VariableOneToOneMapping2 method runTests.

public void runTests() {
    /**
     ***********************************************************************
     */
    mapping.addClassIndicator(Employee.class, null);
    if (mapping.getClassIndicatorAssociations().isEmpty()) {
        testFailures += "addClassIndicator = null did not add a null wrapper type indicator";
    } else {
        Enumeration e = mapping.getClassIndicatorAssociations().elements();
        while (e.hasMoreElements()) {
            TypedAssociation association = (TypedAssociation) e.nextElement();
            if (association.getKey() == Employee.class) {
                if (!(association.getValue() instanceof Helper)) {
                    testFailures += "addClassIndicator = null, type indicator does not = Helper class";
                }
            }
        }
    }
    /**
     ***********************************************************************
     */
    Vector vectorIn = new Vector();
    vectorIn.add(new Association(Actor.class, new String("ASHLEY JUDD")));
    vectorIn.add(new Association(Secretary.class, new String("DARTH VADER")));
    vectorIn.add(new Association(Broadcastor.class, new String("RED KELLY")));
    mapping.setClassIndicatorAssociations(vectorIn);
    Vector vectorOut = mapping.getClassIndicatorAssociations();
    if (vectorOut.size() != 3) {
        testFailures += "setClassIndicatorAssociations - the set failed";
    } else {
        int foundCount = 0;
        for (int i = 0; i < vectorOut.size(); i++) {
            Association ass = (Association) vectorOut.elementAt(i);
            if (ass.getKey() == Actor.class.getName() && ass.getValue().equals("ASHLEY JUDD")) {
                foundCount++;
            }
            if (ass.getKey() == Secretary.class.getName() && ass.getValue().equals("DARTH VADER")) {
                foundCount++;
            }
            if (ass.getKey() == Broadcastor.class.getName() && ass.getValue().equals("RED KELLY")) {
                foundCount++;
            }
        }
        if (foundCount != 3) {
            testFailures += "setClassIndicatorAssociations - association values not found";
        }
    }
    /**
     ***********************************************************************
     */
    Association assoc = new Association(new String("key"), new String("value"));
    Vector in = new Vector();
    in.add(assoc);
    mapping.setSourceToTargetQueryKeyFieldAssociations(in);
    Vector out = mapping.getSourceToTargetQueryKeyFieldAssociations();
    if (out.size() != 1) {
        testFailures += "setSourceToTargetQueryFieldAssociations - the set failed";
    } else {
        Association a = (Association) out.elementAt(0);
        if (!(a.getKey().equals("key") && a.getValue().equals("value"))) {
            testFailures += "setSourceToTargetQueryFieldAssociations - value in the set failed";
        }
    }
    /**
     ***********************************************************************
     */
    Vector foreignKeyNames = new Vector();
    foreignKeyNames.add("fkey1");
    foreignKeyNames.add("fkey2");
    foreignKeyNames.add("fkey3");
    mapping.setForeignKeyFieldNames(foreignKeyNames);
    Vector fieldNames = mapping.getForeignKeyFieldNames();
    if (!(mapping.getForeignKeyFieldNames().contains("fkey1"))) {
        testFailures += "addForeignQueryKeyName - fkey1";
    }
    if (!(mapping.getForeignKeyFieldNames().contains("fkey2"))) {
        testFailures += "addForeignQueryKeyName - fkey2";
    }
    if (!(mapping.getForeignKeyFieldNames().contains("fkey3"))) {
        testFailures += "addForeignQueryKeyName - fkey3";
    }
    /**
     ***********************************************************************
     */
    mapping.setTypeFieldName("doesNotExist");
    if (!mapping.getTypeFieldName().equals("doesNotExist")) {
        testFailures += "setTypeFieldName failed";
    }
    /**
     ***********************************************************************
     */
    // tests done through the wrapper
    /**
     ***********************************************************************
     */
    /**
     ***********************************************************************
     */
    VariableOneToOneMapping2 wrappedMapping = new VariableOneToOneMapping2();
    wrappedMapping.setTypeField(null);
    if (wrappedMapping.getTypeFieldName() != null) {
        testFailures += "setTypeField - set to null failed";
    }
    /**
     ***********************************************************************
     */
    wrappedMapping.addClassIndicator(Employee.class, null);
    if (wrappedMapping.getTypeForImplementor(Employee.class) != null) {
        testFailures += "getTypeForImplementor failed";
    }
    if (wrappedMapping.getImplementorForType(null, getSession()) != Employee.class) {
        testFailures += "getImplementorForType failed";
    }
}
Also used : Helper(org.eclipse.persistence.internal.helper.Helper) Enumeration(java.util.Enumeration) TypedAssociation(org.eclipse.persistence.mappings.TypedAssociation) Association(org.eclipse.persistence.mappings.Association) TypedAssociation(org.eclipse.persistence.mappings.TypedAssociation) Vector(java.util.Vector)

Aggregations

Association (org.eclipse.persistence.mappings.Association)25 ArrayList (java.util.ArrayList)13 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)13 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)12 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)12 XMLCompositeCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping)12 Iterator (java.util.Iterator)11 List (java.util.List)11 TypedAssociation (org.eclipse.persistence.mappings.TypedAssociation)11 Vector (java.util.Vector)10 PropertyAssociation (org.eclipse.persistence.mappings.PropertyAssociation)8 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 IndirectionPolicy (org.eclipse.persistence.internal.indirection.IndirectionPolicy)7 NoIndirectionPolicy (org.eclipse.persistence.internal.indirection.NoIndirectionPolicy)7 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)7 BasicIndirectionPolicy (org.eclipse.persistence.internal.indirection.BasicIndirectionPolicy)6 ContainerIndirectionPolicy (org.eclipse.persistence.internal.indirection.ContainerIndirectionPolicy)6 ProxyIndirectionPolicy (org.eclipse.persistence.internal.indirection.ProxyIndirectionPolicy)6