Package org.eclipse.persistence.mappings

Examples of org.eclipse.persistence.mappings.DatabaseMapping


                List<DatabaseMapping> batchedMappings = getDescriptor().getObjectBuilder().getBatchFetchedAttributes();
                this.batchFetchPolicy.setMappingQueries(new HashMap(batchedMappings.size()));
                initialized = true;
                int size = batchedMappings.size();
                for (int index = 0; index < size; index++) {
                    DatabaseMapping mapping = batchedMappings.get(index);
                    if ((mapping != null) && mapping.isForeignReferenceMapping()) {
                        // A nested query must be built to pass to the descriptor that looks like the real query execution would.
                        ReadQuery nestedQuery = ((ForeignReferenceMapping)mapping).prepareNestedBatchQuery(this);   
                        // Register the nested query to be used by the mapping for all the objects.
                        this.batchFetchPolicy.getMappingQueries().put(mapping, nestedQuery);
                    }
View Full Code Here


            }
            this.batchFetchPolicy.getAttributes().add(baseExpression.getName());
           
            // Ignore nested
            if (objectExpression.getBaseExpression().isExpressionBuilder()) {
                DatabaseMapping mapping = objectExpression.getMapping();
                if ((mapping != null) && mapping.isForeignReferenceMapping()) {
                    // A nested query must be built to pass to the descriptor that looks like the real query execution would.
                    ReadQuery nestedQuery = ((ForeignReferenceMapping)mapping).prepareNestedBatchQuery(this);   
                    // Register the nested query to be used by the mapping for all the objects.
                    this.batchFetchPolicy.getMappingQueries().put(mapping, nestedQuery);
                }
View Full Code Here

    protected Object updateDerivedIds(Object clone, ClassDescriptor descriptor) {
        Object key = null;
       
        if (descriptor.hasDerivedId()) {
            for (DatabaseMapping derivesIdMapping : descriptor.getDerivesIdMappinps()) {
                DatabaseMapping derivedIdMapping = derivesIdMapping.getDerivedIdMapping();
               
                // If there is no derived id mapping, then there is no update required. Case #1a-#6a
                // from the JPA spec.
                if (derivedIdMapping != null) {
                    ClassDescriptor parentDescriptor = derivesIdMapping.getReferenceDescriptor();
                    Object parentClone = derivesIdMapping.getRealAttributeValueFromObject(clone, this);
                   
                    // If the parent clone is null, we don't have any work to do, continue to the next
                    // mapping. Some mappings may be part of a composite primary key that allows for a
                    // null setting or the mapping may just not be set.
                    if (parentClone != null) {
                        // Recurse up the chain to figure out the key. The first dependent will figure
                        // it out and pass it to its sub-dependents (keeping it the same)
                        if (parentDescriptor.hasDerivedId()) {
                            key = updateDerivedIds(parentClone, parentDescriptor);
                        } else {
                            key = parentDescriptor.getCMPPolicy().createPrimaryKeyInstance(parentClone, this);
                        }
                       
                        if (derivesIdMapping.hasMapsIdValue()) {
                            // Case #1b, #2b and #3b from the JPA spec. The derived id is within our
                            // embedded id. We need to deal with that object and its mapping within the clone.
                            Object aggregateClone = derivedIdMapping.getRealAttributeValueFromObject(clone, this);
                           
                            // If the aggregate clone is null, create one and set it on the clone.
                            if (aggregateClone == null) {
                                aggregateClone = derivedIdMapping.getReferenceDescriptor().getObjectBuilder().buildNewInstance();
                                derivedIdMapping.setRealAttributeValueInObject(clone, aggregateClone);
                            }
                           
                            // Now get the actual derived id mapping from the aggregate and populate it on the aggregate clone.
                            DatabaseMapping aggregateMapping = derivedIdMapping.getReferenceDescriptor().getObjectBuilder().getMappingForAttributeName(derivesIdMapping.getMapsIdValue());
                            aggregateMapping.setRealAttributeValueInObject(aggregateClone, key);
                           
                            // The key should be the aggregate clone when we are done.
                            key = aggregateClone;
                        } else {
                            // Case #4b, #5b, #6b from the JPA spec. Our id mapping is the derived id.
View Full Code Here

                       }
                   }
                   ObjectBuilder builder = getCurrentDescriptor().getObjectBuilder();
                   Iterator<String> it = getCurrentGroup().getAttributeNames().iterator();
                   while(it.hasNext()) {
                       DatabaseMapping mapping = builder.getMappingForAttributeName(it.next());
                       // instantiate indirection
                       mapping.instantiateAttribute(object, session);
                   }
               }
           }
       };
       iterator.setSession(this);
View Full Code Here

    }

    public void writeOutMappings(XMLRecord row, Object object, AbstractSession session) {
        List<DatabaseMapping> mappings = getDescriptor().getMappings();
        for (int index = 0; index < mappings.size(); index++) {
            DatabaseMapping mapping = mappings.get(index);
            mapping.writeFromObjectIntoRow(object, row, session, WriteType.UNDEFINED);
        }
    }
View Full Code Here

        getEagerMappings().clear();
        getRelationshipMappings().clear();

        for (Enumeration mappings = this.descriptor.getMappings().elements();
                 mappings.hasMoreElements();) {
            DatabaseMapping mapping = (DatabaseMapping)mappings.nextElement();

            // Add attribute to mapping association
            if (!mapping.isWriteOnly()) {
                getMappingsByAttribute().put(mapping.getAttributeName(), mapping);
            }
            // Cache mappings that require cloning.
            if (mapping.isCloningRequired()) {
                getCloningMappings().add(mapping);
            }
            // Cache eager mappings.
            if (mapping.isForeignReferenceMapping() && ((ForeignReferenceMapping)mapping).usesIndirection() && (!mapping.isLazy())) {
                getEagerMappings().add(mapping);
            }
            // Cache relationship mappings.
            if (!mapping.isDirectToFieldMapping()) {
                getRelationshipMappings().add(mapping);
            }

            // Add field to mapping association
            for (DatabaseField field : mapping.getFields()) {

                if (mapping.isReadOnly()) {
                    List readOnlyMappings = getReadOnlyMappingsByField().get(field);

                    if (readOnlyMappings == null) {
                        readOnlyMappings = new ArrayList();
                        getReadOnlyMappingsByField().put(field, readOnlyMappings);
                    }

                    readOnlyMappings.add(mapping);
                } else {
                    if (mapping.isAggregateObjectMapping()) {
                        // For Embeddable class, we need to test read-only
                        // status of individual fields in the embeddable.
                        ObjectBuilder aggregateObjectBuilder = ((AggregateObjectMapping)mapping).getReferenceDescriptor().getObjectBuilder();

                        // Look in the non-read-only fields mapping
                        DatabaseMapping aggregatedFieldMapping = aggregateObjectBuilder.getMappingForField(field);

                        if (aggregatedFieldMapping == null) { // mapping must be read-only
                            List readOnlyMappings = getReadOnlyMappingsByField().get(field);

                            if (readOnlyMappings == null) {
                                readOnlyMappings = new ArrayList();
                                getReadOnlyMappingsByField().put(field, readOnlyMappings);
                            }

                            readOnlyMappings.add(mapping);
                        } else {
                            getMappingsByField().put(field, mapping);
                        }
                    } else { // Not an embeddable mapping
                        if (!getMappingsByField().containsKey(field)) {
                            getMappingsByField().put(field, mapping);
                        }
                    }
                }
            }
        }
        this.isSimple = getRelationshipMappings().isEmpty();

        initializePrimaryKey(session);
        initializeJoinedAttributes();

        if (this.descriptor.usesSequenceNumbers()) {
            DatabaseMapping sequenceMapping = getMappingForField(this.descriptor.getSequenceNumberField());
            if ((sequenceMapping != null) && sequenceMapping.isDirectToFieldMapping()) {
                setSequenceMapping((AbstractDirectMapping)sequenceMapping);
            }
        }
    }
View Full Code Here

        if (criteriaBase != null && (subQuery.getItems() == null || subQuery.getItems().isEmpty())){
            if (baseExpression.getSession() != null && ((ObjectExpression)baseExpression).getDescriptor() != null){
                Class sourceClass = ((ObjectExpression)baseExpression).getDescriptor().getJavaClass();
                ClassDescriptor descriptor = baseExpression.getSession().getDescriptor(sourceClass);
                if (descriptor != null){
                    DatabaseMapping mapping = descriptor.getMappingForAttributeName(attribute);
                    if (mapping != null && mapping.isDirectCollectionMapping()){
                        subQuery.setExpressionBuilder(baseExpression.getBuilder());
                        subQuery.setReferenceClass(sourceClass);
                        subQuery.addCount(attribute, subQuery.getExpressionBuilder().anyOf(attribute), returnType);
                        return;
                    }
View Full Code Here

        boolean outerJoin = false;
        ClassDescriptor newDescriptor = null;
        if (baseExp.isQueryKeyExpression()) {
            // now need to find out if it is a direct to field or something
            // else.
            DatabaseMapping mapping = getLeafMappingFor(baseExp, ((QueryKeyExpression) baseExp).descriptor);
            if ((mapping != null) && !mapping.isDirectToFieldMapping()) {
                outerJoin = ((QueryKeyExpression) baseExp).shouldUseOuterJoin();
                if (mapping.isAggregateMapping()) {
                    newDescriptor = mapping.getDescriptor();
                    baseExp = ((QueryKeyExpression) baseExp).getBaseExpression();
                } else {
                    newDescriptor = mapping.getReferenceDescriptor();
                }
            } else {
                QueryKey queryKey = getLeafQueryKeyFor(baseExp, ((QueryKeyExpression) baseExp).descriptor);
                if ((queryKey != null) && !queryKey.isDirectQueryKey()){
                    outerJoin = ((QueryKeyExpression) baseExp).shouldUseOuterJoin();
                    newDescriptor = queryKey.getDescriptor();
                }
            }
        } else if (baseExp.isExpressionBuilder()) {
            newDescriptor = normalizer.getSession().getDescriptor(((ExpressionBuilder) baseExp).getQueryClass());
        }

        if (newDescriptor != null) {
            // At this point we are committed to rewriting the query.
            if (newDescriptor.hasSimplePrimaryKey() && (newDescriptor.getPrimaryKeyFields().size() == 1)) {
                // case 1: simple PK =>
                // treat COUNT(entity) as COUNT(entity.pk)
                DatabaseMapping pk = getMappingOfFirstPrimaryKey(newDescriptor);
                Expression countArg = baseExp.get(pk.getAttributeName());
                if (distinctUsed) {
                    countArg = countArg.distinct();
                }
                this.setBaseExpression(countArg);
                this.children.setElementAt(countArg, 0);
            } else if (!distinctUsed) {
                // case 2: composite PK, but no DISTINCT =>
                // pick a PK column for the COUNT aggregate
                DatabaseMapping pk = getMappingOfFirstPrimaryKey(newDescriptor);
                Expression countArg = baseExp.get(pk.getAttributeName());
                while (pk.isAggregateObjectMapping()) {
                    newDescriptor = ((AggregateObjectMapping) pk).getReferenceDescriptor();
                    pk = getMappingOfFirstPrimaryKey(newDescriptor);
                    countArg = countArg.get(pk.getAttributeName());
                }
                this.setBaseExpression(countArg);
                this.children.setElementAt(countArg, 0);
            } else if (!outerJoin) {
                // case 3: composite PK and DISTINCT, but no
View Full Code Here

        Expression baseExpression = ((QueryKeyExpression)expression).getBaseExpression();
        ClassDescriptor baseDescriptor = getLeafDescriptorFor(baseExpression, rootDescriptor);
        ClassDescriptor descriptor = null;
        String attributeName = expression.getName();

        DatabaseMapping mapping = baseDescriptor.getObjectBuilder().getMappingForAttributeName(attributeName);

        if (mapping == null) {
            QueryKey queryKey = baseDescriptor.getQueryKeyNamed(attributeName);
            if (queryKey != null) {
                if (queryKey.isForeignReferenceQueryKey()) {
                    descriptor = getSession().getDescriptor(((ForeignReferenceQueryKey)queryKey).getReferenceClass());
                } else// if (queryKey.isDirectQueryKey())
                 {
                    descriptor = queryKey.getDescriptor();
                }
            }
            if (descriptor == null) {
                throw QueryException.invalidQueryKeyInExpression(attributeName);
            }
        } else if (mapping.isAggregateObjectMapping()) {
            descriptor = ((AggregateObjectMapping)mapping).getReferenceDescriptor();
        } else if (mapping.isForeignReferenceMapping()) {
            descriptor = ((ForeignReferenceMapping)mapping).getReferenceDescriptor();
        }
        return descriptor;
    }
View Full Code Here

    }

    protected DatabaseMapping getMappingOfFirstPrimaryKey(ClassDescriptor descriptor) {
        if (descriptor != null) {
            for (Iterator i = descriptor.getMappings().iterator(); i.hasNext(); ) {
                DatabaseMapping m = (DatabaseMapping)i.next();
                if (m.isPrimaryKeyMapping()) {
                    return m;
                }
            }
        }
        return null;
View Full Code Here

TOP

Related Classes of org.eclipse.persistence.mappings.DatabaseMapping

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.