Search in sources :

Example 1 with MappingManager

use of org.datanucleus.store.rdbms.mapping.MappingManager in project datanucleus-rdbms by datanucleus.

the class EmbeddedMapping method addMappingForMember.

/**
 * Method to add a mapping for the specified member to this mapping.
 * @param embCmd Class that the member belongs to
 * @param embMmd Member to be added
 * @param embMmds The metadata defining mapping information for the members (if any)
 */
private void addMappingForMember(AbstractClassMetaData embCmd, AbstractMemberMetaData embMmd, AbstractMemberMetaData[] embMmds) {
    if (emd != null && emd.getOwnerMember() != null && emd.getOwnerMember().equals(embMmd.getName())) {
    // Do nothing since we don't map owner fields (since the owner is the containing object)
    } else {
        AbstractMemberMetaData embeddedMmd = null;
        for (int j = 0; j < embMmds.length; j++) {
            // Why are these even possible ? Why are they here ? Why don't they use localised messages ?
            if (embMmds[j] == null) {
                throw new RuntimeException("embMmds[j] is null for class=" + embCmd.toString() + " type=" + typeName);
            }
            AbstractMemberMetaData embMmdForMmds = embCmd.getMetaDataForMember(embMmds[j].getName());
            if (embMmdForMmds != null) {
                if (embMmdForMmds.getAbsoluteFieldNumber() == embMmd.getAbsoluteFieldNumber()) {
                    // Same as the member we are processing, so use it
                    embeddedMmd = embMmds[j];
                }
            }
        }
        // Add mapping
        JavaTypeMapping embMmdMapping;
        MappingManager mapMgr = table.getStoreManager().getMappingManager();
        if (embeddedMmd != null) {
            // User has provided a field definition so map with that
            embMmdMapping = mapMgr.getMapping(table, embeddedMmd, clr, FieldRole.ROLE_FIELD);
        } else {
            // User hasn't provided a field definition so map with the classes own definition
            embMmdMapping = mapMgr.getMapping(table, embMmd, clr, FieldRole.ROLE_FIELD);
        }
        if (embMmd.getRelationType(clr) != RelationType.NONE && embMmdMapping instanceof AbstractContainerMapping) {
            // TODO Support 1-N (unidirectional) relationships and use owner object as the key in the join table
            NucleusLogger.PERSISTENCE.warn("Embedded object at " + getMemberMetaData().getFullFieldName() + " has a member " + embMmd.getFullFieldName() + " that is a container. Not fully supported as part of an embedded object!");
        }
        // Use field number from embMmd, since the embedded mapping info doesn't have reliable field number infos
        embMmdMapping.setAbsFieldNumber(embMmd.getAbsoluteFieldNumber());
        this.addJavaTypeMapping(embMmdMapping);
        for (int j = 0; j < embMmdMapping.getNumberOfDatastoreMappings(); j++) {
            // Register column with mapping
            DatastoreMapping datastoreMapping = embMmdMapping.getDatastoreMapping(j);
            this.addDatastoreMapping(datastoreMapping);
            if (this.mmd.isPrimaryKey()) {
                // Overall embedded field should be part of PK, so make all datastore fields part of it
                Column col = datastoreMapping.getColumn();
                if (col != null) {
                    col.setPrimaryKey();
                }
            }
        }
    }
}
Also used : DatastoreMapping(org.datanucleus.store.rdbms.mapping.datastore.DatastoreMapping) Column(org.datanucleus.store.rdbms.table.Column) MappingManager(org.datanucleus.store.rdbms.mapping.MappingManager) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 2 with MappingManager

use of org.datanucleus.store.rdbms.mapping.MappingManager in project datanucleus-rdbms by datanucleus.

the class AbstractContainerMapping method prepareDatastoreMapping.

/**
 * Method to prepare a field mapping for use in the datastore.
 * This creates the column in the table.
 */
protected void prepareDatastoreMapping() {
    if (containerIsStoredInSingleColumn()) {
        // Serialised collections/maps/arrays should just create a (typically BLOB) column as normal in the owning table
        MappingManager mmgr = storeMgr.getMappingManager();
        ColumnMetaData colmd = null;
        ColumnMetaData[] colmds = mmd.getColumnMetaData();
        if (colmds != null && colmds.length > 0) {
            // Try the field column info
            colmd = colmds[0];
        } else if (mmd.hasCollection() || mmd.hasArray()) {
            // Fallback to the element column info
            colmds = (mmd.getElementMetaData() != null) ? mmd.getElementMetaData().getColumnMetaData() : null;
            if (colmds != null && colmds.length > 0) {
                colmd = colmds[0];
            }
        }
        Column col = mmgr.createColumn(this, getJavaTypeForDatastoreMapping(0), colmd);
        mmgr.createDatastoreMapping(this, mmd, 0, col);
    }
}
Also used : Column(org.datanucleus.store.rdbms.table.Column) MappingManager(org.datanucleus.store.rdbms.mapping.MappingManager) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData)

Example 3 with MappingManager

use of org.datanucleus.store.rdbms.mapping.MappingManager in project datanucleus-rdbms by datanucleus.

the class ReferenceMapping method prepareDatastoreMapping.

/**
 * Convenience method to create the necessary columns to represent this reference in the datastore.
 * With "per-implementation" mapping strategy will create columns for each of the possible implementations.
 * With "identity"/"xcalia" will create a single column to store a reference to the implementation value.
 * @param clr The ClassLoaderResolver
 */
protected void prepareDatastoreMapping(ClassLoaderResolver clr) {
    if (mappingStrategy == PER_IMPLEMENTATION_MAPPING) {
        // Mapping per reference implementation, so create columns for each possible implementation
        if (roleForMember == FieldRole.ROLE_ARRAY_ELEMENT) {
            // Creation of columns in join table for array of references
            ColumnMetaData[] colmds = null;
            ElementMetaData elemmd = mmd.getElementMetaData();
            if (elemmd != null && elemmd.getColumnMetaData() != null && elemmd.getColumnMetaData().length > 0) {
                // Column mappings defined at this side (1-N, M-N)
                colmds = elemmd.getColumnMetaData();
            }
            createPerImplementationColumnsForReferenceField(false, false, false, false, roleForMember, colmds, clr);
        } else if (roleForMember == FieldRole.ROLE_COLLECTION_ELEMENT) {
            // Creation of columns in join table for collection of references
            ColumnMetaData[] colmds = null;
            AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
            ElementMetaData elemmd = mmd.getElementMetaData();
            if (elemmd != null && elemmd.getColumnMetaData() != null && elemmd.getColumnMetaData().length > 0) {
                // Column mappings defined at this side (1-N, M-N)
                colmds = elemmd.getColumnMetaData();
            } else if (relatedMmds != null && relatedMmds[0].getJoinMetaData() != null && relatedMmds[0].getJoinMetaData().getColumnMetaData() != null && relatedMmds[0].getJoinMetaData().getColumnMetaData().length > 0) {
                // Column mappings defined at other side (M-N) on <join>
                colmds = relatedMmds[0].getJoinMetaData().getColumnMetaData();
            }
            createPerImplementationColumnsForReferenceField(false, false, false, false, roleForMember, colmds, clr);
        } else if (roleForMember == FieldRole.ROLE_MAP_KEY) {
            // Creation of columns in join table for map of references as keys
            ColumnMetaData[] colmds = null;
            KeyMetaData keymd = mmd.getKeyMetaData();
            if (keymd != null && keymd.getColumnMetaData() != null && keymd.getColumnMetaData().length > 0) {
                // Column mappings defined at this side (1-N, M-N)
                colmds = keymd.getColumnMetaData();
            }
            createPerImplementationColumnsForReferenceField(false, false, false, false, roleForMember, colmds, clr);
        } else if (roleForMember == FieldRole.ROLE_MAP_VALUE) {
            // Creation of columns in join table for map of references as values
            ColumnMetaData[] colmds = null;
            ValueMetaData valuemd = mmd.getValueMetaData();
            if (valuemd != null && valuemd.getColumnMetaData() != null && valuemd.getColumnMetaData().length > 0) {
                // Column mappings defined at this side (1-N, M-N)
                colmds = valuemd.getColumnMetaData();
            }
            createPerImplementationColumnsForReferenceField(false, false, false, false, roleForMember, colmds, clr);
        } else {
            if (mmd.getMappedBy() == null) {
                // Unidirectional 1-1
                boolean embedded = (mmd.isEmbedded() || mmd.getEmbeddedMetaData() != null);
                createPerImplementationColumnsForReferenceField(false, true, false, embedded, roleForMember, mmd.getColumnMetaData(), clr);
            } else {
                // Bidirectional 1-1/N-1
                AbstractClassMetaData refCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForInterface(mmd.getType(), clr);
                if (refCmd != null && refCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
                    // TODO Is this block actually reachable ? Would we specify "inheritance" under "interface" elements?
                    // Find the actual tables storing the other end (can be multiple subclasses)
                    AbstractClassMetaData[] cmds = storeMgr.getClassesManagingTableForClass(refCmd, clr);
                    if (cmds != null && cmds.length > 0) {
                        if (cmds.length > 1) {
                            NucleusLogger.PERSISTENCE.warn("Field " + mmd.getFullFieldName() + " represents either a 1-1 relation, or a N-1 relation where the other end uses" + " \"subclass-table\" inheritance strategy and more than 1 subclasses with a table. " + "This is not fully supported currently");
                        }
                    } else {
                        // TODO Throw an exception ?
                        return;
                    }
                    // TODO We need a mapping for each of the possible subclass tables
                    /*JavaTypeMapping referenceMapping = */
                    storeMgr.getDatastoreClass(cmds[0].getFullClassName(), clr).getIdMapping();
                } else {
                    String[] implTypes = MetaDataUtils.getInstance().getImplementationNamesForReferenceField(mmd, FieldRole.ROLE_FIELD, clr, storeMgr.getMetaDataManager());
                    for (int j = 0; j < implTypes.length; j++) {
                        JavaTypeMapping refMapping = storeMgr.getDatastoreClass(implTypes[j], clr).getIdMapping();
                        JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(clr.classForName(implTypes[j]));
                        mapping.setReferenceMapping(refMapping);
                        this.addJavaTypeMapping(mapping);
                    }
                }
            }
        }
    } else if (mappingStrategy == ID_MAPPING || mappingStrategy == XCALIA_MAPPING) {
        // Single (String) column storing the identity of the related object
        MappingManager mapMgr = storeMgr.getMappingManager();
        JavaTypeMapping mapping = mapMgr.getMapping(String.class);
        mapping.setMemberMetaData(mmd);
        mapping.setTable(table);
        mapping.setRoleForMember(roleForMember);
        Column col = mapMgr.createColumn(mapping, String.class.getName(), 0);
        mapMgr.createDatastoreMapping(mapping, mmd, 0, col);
        this.addJavaTypeMapping(mapping);
    }
}
Also used : ElementMetaData(org.datanucleus.metadata.ElementMetaData) KeyMetaData(org.datanucleus.metadata.KeyMetaData) Column(org.datanucleus.store.rdbms.table.Column) ValueMetaData(org.datanucleus.metadata.ValueMetaData) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) MappingManager(org.datanucleus.store.rdbms.mapping.MappingManager) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 4 with MappingManager

use of org.datanucleus.store.rdbms.mapping.MappingManager in project datanucleus-rdbms by datanucleus.

the class SerialisedKeyPCMapping method prepareDatastoreMapping.

/**
 * Method to prepare a field mapping for use in the datastore.
 * This creates the column in the table.
 */
protected void prepareDatastoreMapping() {
    MappingManager mmgr = storeMgr.getMappingManager();
    ColumnMetaData colmd = null;
    if (mmd.getKeyMetaData() != null && mmd.getKeyMetaData().getColumnMetaData() != null && mmd.getKeyMetaData().getColumnMetaData().length > 0) {
        colmd = mmd.getKeyMetaData().getColumnMetaData()[0];
    }
    Column col = mmgr.createColumn(this, getType(), colmd);
    mmgr.createDatastoreMapping(this, mmd, 0, col);
}
Also used : Column(org.datanucleus.store.rdbms.table.Column) MappingManager(org.datanucleus.store.rdbms.mapping.MappingManager) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData)

Example 5 with MappingManager

use of org.datanucleus.store.rdbms.mapping.MappingManager in project datanucleus-rdbms by datanucleus.

the class SerialisedValuePCMapping method prepareDatastoreMapping.

/**
 * Method to prepare a field mapping for use in the datastore.
 * This creates the column in the table.
 */
protected void prepareDatastoreMapping() {
    MappingManager mmgr = storeMgr.getMappingManager();
    ColumnMetaData colmd = null;
    if (mmd.getValueMetaData() != null && mmd.getValueMetaData().getColumnMetaData() != null && mmd.getValueMetaData().getColumnMetaData().length > 0) {
        colmd = mmd.getValueMetaData().getColumnMetaData()[0];
    }
    Column col = mmgr.createColumn(this, getType(), colmd);
    mmgr.createDatastoreMapping(this, mmd, 0, col);
}
Also used : Column(org.datanucleus.store.rdbms.table.Column) MappingManager(org.datanucleus.store.rdbms.mapping.MappingManager) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData)

Aggregations

MappingManager (org.datanucleus.store.rdbms.mapping.MappingManager)12 Column (org.datanucleus.store.rdbms.table.Column)10 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)8 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)2 DatastoreMapping (org.datanucleus.store.rdbms.mapping.datastore.DatastoreMapping)2 Timestamp (java.sql.Timestamp)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1 ClassMetaData (org.datanucleus.metadata.ClassMetaData)1 DiscriminatorMetaData (org.datanucleus.metadata.DiscriminatorMetaData)1 ElementMetaData (org.datanucleus.metadata.ElementMetaData)1 KeyMetaData (org.datanucleus.metadata.KeyMetaData)1 RelationType (org.datanucleus.metadata.RelationType)1 ValueMetaData (org.datanucleus.metadata.ValueMetaData)1 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)1 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)1 CorrespondentColumnsMapper (org.datanucleus.store.rdbms.mapping.CorrespondentColumnsMapper)1