Package org.eclipse.persistence.mappings

Examples of org.eclipse.persistence.mappings.ManyToManyMapping


     * INTERNAL:
     */
    protected void prepareManyToManySelectionQuery(ManyToManyMapping sourceMapping, AbstractSession session) {
        // Clone the mapping because in reality that is what we have, that
        // is, a M-M mapping to each class of the hierarchy.
        ManyToManyMapping manyToMany = (ManyToManyMapping) sourceMapping.clone();
       
        // Update the foreign key fields on the mapping. Basically, take the
        // table name off and let the descriptor figure it out.
        for (DatabaseField keyField : manyToMany.getTargetKeyFields()) {
            keyField.setTable(new DatabaseTable());
        }
       
        addSelectionQuery(manyToMany, sourceMapping, session);
    }
View Full Code Here


    @Override
    public void process() {
        super.process();
       
        // Create a M-M mapping and process common collection mapping metadata.
        ManyToManyMapping mapping = new ManyToManyMapping();
        process(mapping);

        if (getMappedBy() == null || getMappedBy().equals("")) {
            // Processing the owning side of a M-M that is process a join table.
            processJoinTable(mapping);
        } else {
            // We are processing the a non-owning side of a M-M. Must set the
            // mapping read-only.
            mapping.setIsReadOnly(true);
           
            // Get the owning mapping from the reference descriptor metadata.
            ManyToManyMapping ownerMapping = null;
            if (getOwningMapping(getMappedBy()).isManyToManyMapping()){
                ownerMapping = (ManyToManyMapping)getOwningMapping(getMappedBy());
            } else {
                // If improper mapping encountered, throw an exception.
                throw ValidationException.invalidMapping(getJavaClass(), getReferenceClass());
            }

            // Set the relation table name from the owner.
            mapping.setRelationTable(ownerMapping.getRelationTable());
                
            // In a table per class inheritance we need to update the target
            // keys before setting them to mapping's source key fields.
            if (getDescriptor().usesTablePerClassInheritanceStrategy()) {
                // Update the target key fields.
                Vector<DatabaseField> targetKeyFields = new Vector<DatabaseField>();
                for (DatabaseField targetKeyField : ownerMapping.getTargetKeyFields()) {
                    DatabaseField newTargetKeyField = (DatabaseField) targetKeyField.clone();
                    newTargetKeyField.setTable(getDescriptor().getPrimaryTable());
                    targetKeyFields.add(newTargetKeyField);
                }
               
                mapping.setSourceKeyFields(targetKeyFields);
               
                // Update the targetRelationKeyFields.
                Vector<DatabaseField> targetRelationKeyFields = new Vector<DatabaseField>();
                for (DatabaseField targetRelationKeyField : ownerMapping.getTargetRelationKeyFields()) {
                    DatabaseField newTargetRelationKeyField = (DatabaseField) targetRelationKeyField.clone();
                    newTargetRelationKeyField.setTable(getDescriptor().getPrimaryTable());
                    targetRelationKeyFields.add(newTargetRelationKeyField);
                }
               
                mapping.setSourceRelationKeyFields(targetRelationKeyFields);
            } else {
                // Add all the source foreign keys we found on the owner.
                mapping.setSourceKeyFields(ownerMapping.getTargetKeyFields());
                mapping.setSourceRelationKeyFields(ownerMapping.getTargetRelationKeyFields());
            }
           
            // Add all the target foreign keys we found on the owner.
            mapping.setTargetKeyFields(ownerMapping.getSourceKeyFields());
            mapping.setTargetRelationKeyFields(ownerMapping.getSourceRelationKeyFields());
        }
       
        // Process properties
        processProperties(mapping);
View Full Code Here

                // Add the mapping to the descriptor.
                getDescriptor().addMapping(mapping);
            } else {
                // Create a M-M mapping and process common collection mapping
                // metadata.
                ManyToManyMapping mapping = new ManyToManyMapping();
                process(mapping);
               
                // Process the JoinTable metadata.
                processJoinTable(mapping);
               
                // Process properties
                processProperties(mapping);
   
                // Add the mapping to the descriptor.
                getDescriptor().addMapping(mapping);
            }
        } else {
            // Create a 1-M mapping and process common collection mapping
            // metadata.
            OneToManyMapping mapping = new OneToManyMapping();
            process(mapping);
           
            // Non-owning side, process the foreign keys from the owner.
            OneToOneMapping ownerMapping = null;
            if (getOwningMapping(getMappedBy()).isOneToOneMapping()){
                ownerMapping = (OneToOneMapping) getOwningMapping(getMappedBy());
            } else {
                // If improper mapping encountered, throw an exception.
                throw ValidationException.invalidMapping(getJavaClass(), getReferenceClass());
            }
               
            Map<DatabaseField, DatabaseField> keys = ownerMapping.getSourceToTargetKeyFields();
            for (DatabaseField fkField : keys.keySet()) {
                DatabaseField pkField = keys.get(fkField);
               
                // If we are within a table per class strategy we have to update
                // the primary key field to point to our own database table.
                // The extra table check is if the mapping is actually defined
                // on our java class (meaning we have the right table at this
                // point and can avoid the cloning)
                if (getDescriptor().usesTablePerClassInheritanceStrategy() && ! pkField.getTable().equals(getDescriptor().getPrimaryTable())) {
                    // We need to update the pk field to be to our table.
                    pkField = (DatabaseField) pkField.clone();
                    pkField.setTable(getDescriptor().getPrimaryTable());
                }
           
                mapping.addTargetForeignKeyField(fkField, pkField);
            }  
           
            // Process properties
            processProperties(mapping);
           
View Full Code Here

                }
                return associations;
            }

            public void setAttributeValueInObject(Object object, Object value) {
                ManyToManyMapping mapping = (ManyToManyMapping)object;
                List associations = (List)value;
                mapping.setSourceKeyFields(NonSynchronizedVector.newInstance(associations.size()));
                mapping.setSourceRelationKeyFields(NonSynchronizedVector.newInstance(associations.size()));
                Iterator iterator = associations.iterator();
                while (iterator.hasNext()) {
                    Association association = (Association)iterator.next();
                    mapping.getSourceKeyFields().add((DatabaseField)association.getValue());
                    mapping.getSourceRelationKeyFields().add((DatabaseField)association.getKey());
                }
            }
        });
        sourceToRelationKeyFieldAssociationsMapping.setAttributeName("sourceToRelationKeyFieldAssociationsMapping");
        sourceToRelationKeyFieldAssociationsMapping.setXPath(getSecondaryNamespaceXPath() + "source-relation-foreign-key/" + getSecondaryNamespaceXPath() + "field-reference");
        descriptor.addMapping(sourceToRelationKeyFieldAssociationsMapping);

        XMLCompositeCollectionMapping targetToRelationKeyFieldAssociationsMapping = new XMLCompositeCollectionMapping();
        targetToRelationKeyFieldAssociationsMapping.setReferenceClass(Association.class);
        // Handle translation of foreign key associations to hashtables.
        targetToRelationKeyFieldAssociationsMapping.setAttributeAccessor(new AttributeAccessor() {
            public Object getAttributeValueFromObject(Object object) {
                List targetFields = ((ManyToManyMapping)object).getTargetKeyFields();
                List relationFields = ((ManyToManyMapping)object).getTargetRelationKeyFields();
                List associations = new ArrayList(targetFields.size());
                for (int index = 0; index < targetFields.size(); index++) {
                    associations.add(new Association(relationFields.get(index), targetFields.get(index)));
                }
                return associations;
            }

            public void setAttributeValueInObject(Object object, Object value) {
                ManyToManyMapping mapping = (ManyToManyMapping)object;
                List associations = (List)value;
                mapping.setTargetKeyFields(NonSynchronizedVector.newInstance(associations.size()));
                mapping.setTargetRelationKeyFields(NonSynchronizedVector.newInstance(associations.size()));
                Iterator iterator = associations.iterator();
                while (iterator.hasNext()) {
                    Association association = (Association)iterator.next();
                    mapping.getTargetKeyFields().add((DatabaseField)association.getValue());
                    mapping.getTargetRelationKeyFields().add((DatabaseField)association.getKey());
                }
            }
        });
        targetToRelationKeyFieldAssociationsMapping.setAttributeName("targetToRelationKeyFieldAssociations");
        targetToRelationKeyFieldAssociationsMapping.setXPath(getSecondaryNamespaceXPath() + "target-relation-foreign-key/" + getSecondaryNamespaceXPath() + "field-reference");
View Full Code Here

                throw ValidationException.uniDirectionalOneToManyHasJoinColumnAnnotations(getAttributeName(), getJavaClass());
            }
           
            // Create a M-M mapping and process common collection mapping
            // metadata.
            ManyToManyMapping mapping = new ManyToManyMapping();
            process(mapping);
           
            // Process the JoinTable metadata.
            processJoinTable(mapping);
           
            // Process properties
            processProperties(mapping);

            // Add the mapping to the descriptor.
            getDescriptor().addMapping(mapping);
        } else {
            // Create a 1-M mapping and process common collection mapping
            // metadata.
            OneToManyMapping mapping = new OneToManyMapping();
            process(mapping);
           
            // Non-owning side, process the foreign keys from the owner.
            OneToOneMapping ownerMapping = null;
            if (getOwningMapping(getMappedBy()).isOneToOneMapping()){
                ownerMapping = (OneToOneMapping) getOwningMapping(getMappedBy());
            } else {
                // If improper mapping encountered, throw an exception.
                throw ValidationException.invalidMapping(getJavaClass(), getReferenceClass());
            }
               
            Map<DatabaseField, DatabaseField> keys = ownerMapping.getSourceToTargetKeyFields();
            for (DatabaseField fkField : keys.keySet()) {
                mapping.addTargetForeignKeyField(fkField, keys.get(fkField));
            }  
           
            // Process properties
            processProperties(mapping);
           
View Full Code Here

    @Override
    public void process() {
        super.process();
       
        // Create a M-M mapping and process common collection mapping metadata.
        ManyToManyMapping mapping = new ManyToManyMapping();
        process(mapping);

        if (getMappedBy() == null || getMappedBy().equals("")) {
            // Processing the owning side of a M-M that is process a join table.
            processJoinTable(mapping);
        } else {
            // We are processing the a non-owning side of a M-M. Must set the
            // mapping read-only.
            mapping.setIsReadOnly(true);
           
            // Get the owning mapping from the reference descriptor metadata.
            ManyToManyMapping ownerMapping = null;
            if (getOwningMapping(getMappedBy()).isManyToManyMapping()){
                ownerMapping = (ManyToManyMapping)getOwningMapping(getMappedBy());
            } else {
                // If improper mapping encountered, throw an exception.
                throw ValidationException.invalidMapping(getJavaClass(), getReferenceClass());
            }

            // Set the relation table name from the owner.
            mapping.setRelationTable(ownerMapping.getRelationTable());
                
            // Add all the source foreign keys we found on the owner.
            mapping.setSourceKeyFields(ownerMapping.getTargetKeyFields());
            mapping.setSourceRelationKeyFields(ownerMapping.getTargetRelationKeyFields());
               
            // Add all the target foreign keys we found on the owner.
            mapping.setTargetKeyFields(ownerMapping.getSourceKeyFields());
            mapping.setTargetRelationKeyFields(ownerMapping.getSourceRelationKeyFields());
        }
       
        // Process properties
        processProperties(mapping);
View Full Code Here

                // Add the mapping to the descriptor.
                getDescriptor().addMapping(mapping);
            } else {
                // Create a M-M mapping and process common collection mapping
                // metadata.
                ManyToManyMapping mapping = new ManyToManyMapping();
                process(mapping);
               
                // Process the JoinTable metadata.
                processJoinTable(mapping);
               
                // Process properties
                processProperties(mapping);
   
                // Add the mapping to the descriptor.
                getDescriptor().addMapping(mapping);
            }
        } else {
            // Create a 1-M mapping and process common collection mapping
            // metadata.
            OneToManyMapping mapping = new OneToManyMapping();
            process(mapping);
           
            // Non-owning side, process the foreign keys from the owner.
            OneToOneMapping ownerMapping = null;
            if (getOwningMapping(getMappedBy()).isOneToOneMapping()){
                ownerMapping = (OneToOneMapping) getOwningMapping(getMappedBy());
            } else {
                // If improper mapping encountered, throw an exception.
                throw ValidationException.invalidMapping(getJavaClass(), getReferenceClass());
            }
               
            Map<DatabaseField, DatabaseField> keys = ownerMapping.getSourceToTargetKeyFields();
            for (DatabaseField fkField : keys.keySet()) {
                DatabaseField pkField = keys.get(fkField);
               
                // If we are within a table per class strategy we have to update
                // the primary key field to point to our own database table.
                // The extra table check is if the mapping is actually defined
                // on our java class (meaning we have the right table at this
                // point and can avoid the cloning)
                if (getDescriptor().usesTablePerClassInheritanceStrategy() && ! pkField.getTable().equals(getDescriptor().getPrimaryTable())) {
                    // We need to update the pk field to be to our table.
                    pkField = (DatabaseField) pkField.clone();
                    pkField.setTable(getDescriptor().getPrimaryTable());
                }
           
                mapping.addTargetForeignKeyField(fkField, pkField);
            }  
           
            // Process properties
            processProperties(mapping);
           
View Full Code Here

    @Override
    public void process() {
        super.process();
       
        // Create a M-M mapping and process common collection mapping metadata.
        ManyToManyMapping mapping = new ManyToManyMapping();
        process(mapping);

        if (getMappedBy() == null || getMappedBy().equals("")) {
            // Processing the owning side of a M-M that is process a join table.
            processJoinTable(mapping);
        } else {
            // We are processing the a non-owning side of a M-M. Must set the
            // mapping read-only.
            mapping.setIsReadOnly(true);
           
            // Get the owning mapping from the reference descriptor metadata.
            ManyToManyMapping ownerMapping = null;
            if (getOwningMapping(getMappedBy()).isManyToManyMapping()){
                ownerMapping = (ManyToManyMapping)getOwningMapping(getMappedBy());
            } else {
                // If improper mapping encountered, throw an exception.
                throw ValidationException.invalidMapping(getJavaClass(), getReferenceClass());
            }

            // Set the relation table name from the owner.
            mapping.setRelationTable(ownerMapping.getRelationTable());
                
            // In a table per class inheritance we need to update the target
            // keys before setting them to mapping's source key fields.
            if (getDescriptor().usesTablePerClassInheritanceStrategy()) {
                // Update the target key fields.
                Vector<DatabaseField> targetKeyFields = new Vector<DatabaseField>();
                for (DatabaseField targetKeyField : ownerMapping.getTargetKeyFields()) {
                    DatabaseField newTargetKeyField = (DatabaseField) targetKeyField.clone();
                    newTargetKeyField.setTable(getDescriptor().getPrimaryTable());
                    targetKeyFields.add(newTargetKeyField);
                }
               
                mapping.setSourceKeyFields(targetKeyFields);
               
                // Update the targetRelationKeyFields.
                Vector<DatabaseField> targetRelationKeyFields = new Vector<DatabaseField>();
                for (DatabaseField targetRelationKeyField : ownerMapping.getTargetRelationKeyFields()) {
                    DatabaseField newTargetRelationKeyField = (DatabaseField) targetRelationKeyField.clone();
                    newTargetRelationKeyField.setTable(getDescriptor().getPrimaryTable());
                    targetRelationKeyFields.add(newTargetRelationKeyField);
                }
               
                mapping.setSourceRelationKeyFields(targetRelationKeyFields);
            } else {
                // Add all the source foreign keys we found on the owner.
                mapping.setSourceKeyFields(ownerMapping.getTargetKeyFields());
                mapping.setSourceRelationKeyFields(ownerMapping.getTargetRelationKeyFields());
            }
           
            // Add all the target foreign keys we found on the owner.
            mapping.setTargetKeyFields(ownerMapping.getSourceKeyFields());
            mapping.setTargetRelationKeyFields(ownerMapping.getSourceRelationKeyFields());
        }
       
        // Process properties
        processProperties(mapping);
View Full Code Here

     * @param refType
     *            target dynamic entity
     * @param relationshipTableName
     */
    public void addManyToManyMapping(String name, DynamicType refType, String relationshipTableName) {
        ManyToManyMapping mapping = new ManyToManyMapping();
        mapping.setAttributeName(name);
        mapping.setReferenceClass(refType.getJavaClass());
        mapping.setRelationTableName(relationshipTableName);

        for (DatabaseField sourcePK : getType().getDescriptor().getPrimaryKeyFields()) {
            mapping.addSourceRelationKeyFieldName(sourcePK.getName(), sourcePK.getQualifiedName());
        }
        for (DatabaseField targetPK : refType.getDescriptor().getPrimaryKeyFields()) {
            String relField = targetPK.getName();
            if (mapping.getSourceRelationKeyFieldNames().contains(relField)) {
                relField = refType.getName() + "_" + relField;
            }
            mapping.addTargetRelationKeyFieldName(relField, targetPK.getQualifiedName());
        }
        mapping.useTransparentList();

        addMapping(mapping);
    }
View Full Code Here

     * @param refType
     *            target dynamic entity
     * @param relationshipTableName
     */
    public void addManyToManyMapping(String name, DynamicType refType, String relationshipTableName) {
        ManyToManyMapping mapping = new ManyToManyMapping();
        mapping.setAttributeName(name);
        mapping.setReferenceClass(refType.getJavaClass());
        mapping.setRelationTableName(relationshipTableName);

        for (DatabaseField sourcePK : getType().getDescriptor().getPrimaryKeyFields()) {
            mapping.addSourceRelationKeyFieldName(sourcePK.getName(), sourcePK.getQualifiedName());
        }
        for (DatabaseField targetPK : refType.getDescriptor().getPrimaryKeyFields()) {
            String relField = targetPK.getName();
            if (mapping.getSourceRelationKeyFieldNames().contains(relField)) {
                relField = refType.getName() + "_" + relField;
            }
            mapping.addTargetRelationKeyFieldName(relField, targetPK.getQualifiedName());
        }
        mapping.useTransparentList();

        addMapping(mapping);
    }
View Full Code Here

TOP

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

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.