Search in sources :

Example 51 with ColumnMetaData

use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-rdbms by datanucleus.

the class AbstractClassTable method addApplicationIdUsingClassTableId.

/**
 * Utility to create the application identity columns and mapping.
 * Uses the id mapping of the specified class table and copies the mappings
 * and columns, whilst retaining the passed preferences for column namings.
 * This is used to copy the PK mappings of a superclass table so we have the same PK.
 * @param columnContainer The container of column MetaData with any namings
 * @param refTable The table that we use as reference
 * @param clr The ClassLoaderResolver
 * @param cmd The ClassMetaData
 */
final void addApplicationIdUsingClassTableId(ColumnMetaDataContainer columnContainer, DatastoreClass refTable, ClassLoaderResolver clr, AbstractClassMetaData cmd) {
    ColumnMetaData[] userdefinedCols = null;
    int nextUserdefinedCol = 0;
    if (columnContainer != null) {
        userdefinedCols = columnContainer.getColumnMetaData();
    }
    pkMappings = new JavaTypeMapping[cmd.getPKMemberPositions().length];
    for (int i = 0; i < cmd.getPKMemberPositions().length; i++) {
        AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(cmd.getPKMemberPositions()[i]);
        JavaTypeMapping mapping = refTable.getMemberMapping(mmd);
        if (mapping == null) {
            // probably due to invalid metadata defined by the user
            throw new NucleusUserException("Cannot find mapping for field " + mmd.getFullFieldName() + " in table " + refTable.toString() + " " + StringUtils.collectionToString(refTable.getColumns()));
        }
        JavaTypeMapping masterMapping = storeMgr.getMappingManager().getMapping(clr.classForName(mapping.getType()));
        // Update field info in mapping
        masterMapping.setMemberMetaData(mmd);
        masterMapping.setTable(this);
        pkMappings[i] = masterMapping;
        // applying the required names from the columnContainer
        for (int j = 0; j < mapping.getNumberOfDatastoreMappings(); j++) {
            JavaTypeMapping m = masterMapping;
            Column refColumn = mapping.getDatastoreMapping(j).getColumn();
            if (mapping instanceof PersistableMapping) {
                m = storeMgr.getMappingManager().getMapping(clr.classForName(refColumn.getJavaTypeMapping().getType()));
                ((PersistableMapping) masterMapping).addJavaTypeMapping(m);
            }
            ColumnMetaData userdefinedColumn = null;
            if (userdefinedCols != null) {
                for (int k = 0; k < userdefinedCols.length; k++) {
                    if (refColumn.getIdentifier().toString().equals(userdefinedCols[k].getTarget())) {
                        userdefinedColumn = userdefinedCols[k];
                        break;
                    }
                }
                if (userdefinedColumn == null && nextUserdefinedCol < userdefinedCols.length) {
                    userdefinedColumn = userdefinedCols[nextUserdefinedCol++];
                }
            }
            // Add this application identity column
            Column idColumn = null;
            if (userdefinedColumn != null) {
                // User has provided a name for this column
                // Currently we only use the column namings from the users definition but we could easily
                // take more of their details.
                idColumn = addColumn(refColumn.getStoredJavaType(), storeMgr.getIdentifierFactory().newIdentifier(IdentifierType.COLUMN, userdefinedColumn.getName()), m, refColumn.getColumnMetaData());
            } else {
                // No name provided so take same as superclass
                idColumn = addColumn(refColumn.getStoredJavaType(), refColumn.getIdentifier(), m, refColumn.getColumnMetaData());
            }
            if (mapping.getDatastoreMapping(j).getColumn().getColumnMetaData() != null) {
                refColumn.copyConfigurationTo(idColumn);
            }
            idColumn.setPrimaryKey();
            // Set the column type based on the field.getType()
            getStoreManager().getMappingManager().createDatastoreMapping(m, idColumn, refColumn.getJavaTypeMapping().getType());
        }
        // Update highest field number if this is higher
        int absoluteFieldNumber = mmd.getAbsoluteFieldNumber();
        if (absoluteFieldNumber > highestMemberNumber) {
            highestMemberNumber = absoluteFieldNumber;
        }
    }
}
Also used : PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 52 with ColumnMetaData

use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-rdbms by datanucleus.

the class AbstractTable method addColumn.

/**
 * Creates a new column in the table.
 * Will add the new Column and return it. If the new column clashes in name with an existing column of the
 * required name will throw a DuplicateColumnNameException except when :-
 * <ul>
 * <li>The 2 columns are for same named fields in the class or its subclasses with the subclass(es) using
 * "superclass-table" inheritance strategy. One of the columns has to come from a subclass - cant have
 * both from the same class.</li>
 * </ul>
 * @param storedJavaType the java type of the datastore field
 * @param name the SQL identifier for the column to be added
 * @param mapping the mapping for the column to be added
 * @param colmd ColumnMetaData for the column to be added to the table
 * @return the new Column
 * @throws DuplicateColumnException if a column already exists with same name and not a supported situation.
 */
public synchronized Column addColumn(String storedJavaType, DatastoreIdentifier name, JavaTypeMapping mapping, ColumnMetaData colmd) {
    // TODO If already initialized and this is called we should check if exists in current representation
    // then check if exists in datastore, and then create it if necessary
    boolean duplicateName = false;
    if (hasColumnName(name)) {
        duplicateName = true;
    }
    // Create the column
    Column col = new ColumnImpl(this, storedJavaType, name, colmd);
    if (duplicateName && colmd != null) {
        // Verify if a duplicate column is valid. A duplicate column name is (currently) valid when :-
        // 1. subclasses defining the duplicated column are using "super class table" strategy
        // 
        // Find the MetaData for the existing column
        Column existingCol = columnsByIdentifier.get(name);
        MetaData md = existingCol.getColumnMetaData().getParent();
        while (!(md instanceof AbstractClassMetaData)) {
            if (md == null) {
                // ColumnMetaData for existing column has no parent class somehow!
                throw new NucleusUserException(Localiser.msg("057043", name.getName(), getDatastoreIdentifierFullyQualified()));
            }
            md = md.getParent();
        }
        // Find the MetaData for the column to be added
        MetaData dupMd = colmd.getParent();
        while (!(dupMd instanceof AbstractClassMetaData)) {
            dupMd = dupMd.getParent();
            if (dupMd == null) {
                // ColumnMetaData for required column has no parent class somehow!
                throw new NucleusUserException(Localiser.msg("057044", name.getName(), getDatastoreIdentifierFullyQualified(), colmd.toString()));
            }
        }
        boolean reuseColumns = storeMgr.getBooleanProperty(RDBMSPropertyNames.PROPERTY_RDBMS_ALLOW_COLUMN_REUSE);
        if (!reuseColumns) {
            if (((AbstractClassMetaData) md).getFullClassName().equals(((AbstractClassMetaData) dupMd).getFullClassName())) {
                // much sense in most of the cases. (this whole block of duplicated column check, could be optional, like a pmf property)
                throw new DuplicateColumnException(this.toString(), existingCol, col);
            }
            // Make sure the field JavaTypeMappings are compatible
            if (mapping != null && !mapping.getClass().isAssignableFrom(existingCol.getJavaTypeMapping().getClass()) && !existingCol.getJavaTypeMapping().getClass().isAssignableFrom(mapping.getClass())) {
                // the mapping class must be the same (not really required, but to avoid user mistakes)
                throw new DuplicateColumnException(this.toString(), existingCol, col);
            }
        } else {
            if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
                if (mapping != null && mapping.getMemberMetaData() != null) {
                    NucleusLogger.DATASTORE_SCHEMA.debug("Column " + existingCol + " has already been defined but needing to reuse it for " + mapping.getMemberMetaData().getFullFieldName());
                } else {
                    NucleusLogger.DATASTORE_SCHEMA.debug("Column " + existingCol + " has already been defined but needing to reuse it");
                }
            }
        }
        // Make sure the field java types are compatible
        Class fieldStoredJavaTypeClass = null;
        Class existingColStoredJavaTypeClass = null;
        try {
            ClassLoaderResolver clr = storeMgr.getNucleusContext().getClassLoaderResolver(null);
            fieldStoredJavaTypeClass = clr.classForName(storedJavaType);
            existingColStoredJavaTypeClass = clr.classForName(col.getStoredJavaType());
        } catch (RuntimeException cnfe) {
        // Do nothing
        }
        if (fieldStoredJavaTypeClass != null && existingColStoredJavaTypeClass != null && !fieldStoredJavaTypeClass.isAssignableFrom(existingColStoredJavaTypeClass) && !existingColStoredJavaTypeClass.isAssignableFrom(fieldStoredJavaTypeClass)) {
            // the stored java type must be the same (not really required, but to avoid user mistakes)
            throw new DuplicateColumnException(this.toString(), existingCol, col);
        }
    }
    if (!duplicateName) {
        // Only add to our internal list when it is not a dup (since it would try to create a table with the same col twice)
        addColumnInternal(col);
    }
    if (isInitialized()) {
        // Set state to modified
        state = TABLE_STATE_INITIALIZED_MODIFIED;
    }
    return col;
}
Also used : DuplicateColumnException(org.datanucleus.store.rdbms.exceptions.DuplicateColumnException) VersionMetaData(org.datanucleus.metadata.VersionMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) MetaData(org.datanucleus.metadata.MetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) DiscriminatorMetaData(org.datanucleus.metadata.DiscriminatorMetaData) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 53 with ColumnMetaData

use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-rdbms by datanucleus.

the class ClassTable method manageMembers.

/**
 * Goes through all specified members for the specified class and adds a mapping for each.
 * Ignores primary-key fields which are added elsewhere.
 * @param theCmd ClassMetaData for the class to be managed
 * @param clr The ClassLoaderResolver
 * @param mmds the fields/properties to manage
 */
private void manageMembers(AbstractClassMetaData theCmd, ClassLoaderResolver clr, AbstractMemberMetaData[] mmds) {
    // Go through the fields for this class and add columns for them
    for (int fieldNumber = 0; fieldNumber < mmds.length; fieldNumber++) {
        // Primary key fields are added by the initialisePK method
        AbstractMemberMetaData mmd = mmds[fieldNumber];
        if (!mmd.isPrimaryKey()) {
            if (managesMember(mmd.getFullFieldName())) {
                if (!mmd.getClassName(true).equals(theCmd.getFullClassName())) {
                    // Field already managed by this table so maybe we are overriding a superclass
                    JavaTypeMapping fieldMapping = getMappingForMemberName(mmd.getFullFieldName());
                    ColumnMetaData[] colmds = mmd.getColumnMetaData();
                    if (colmds != null && colmds.length > 0) {
                        // Apply this set of ColumnMetaData to the existing mapping
                        int colnum = 0;
                        IdentifierFactory idFactory = getStoreManager().getIdentifierFactory();
                        for (int i = 0; i < fieldMapping.getNumberOfDatastoreMappings(); i++) {
                            Column col = fieldMapping.getDatastoreMapping(i).getColumn();
                            col.setIdentifier(idFactory.newColumnIdentifier(colmds[colnum].getName()));
                            col.setColumnMetaData(colmds[colnum]);
                            colnum++;
                            if (colnum == colmds.length) {
                                // Reached end of specified metadata
                                break;
                            }
                        }
                        // TODO Change this to reflect that we have updated the previous mapping
                        logMapping(mmd.getFullFieldName(), fieldMapping);
                    }
                }
            } else {
                // Manage the field if not already managed (may already exist if overriding a superclass field)
                if (mmd.getPersistenceModifier() == FieldPersistenceModifier.PERSISTENT) {
                    boolean isPrimary = true;
                    if (mmd.getTable() != null && mmd.getJoinMetaData() == null) {
                        // Field has a table specified and is not a 1-N with join table
                        // so is mapped to a secondary table
                        isPrimary = false;
                    }
                    if (isPrimary) {
                        // Add the field to this table
                        JavaTypeMapping fieldMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_FIELD);
                        if (theCmd != cmd && theCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUPERCLASS_TABLE && fieldMapping.getNumberOfDatastoreMappings() > 0) {
                            // Field is for a subclass and so column(s) has to either allow nulls, or have default
                            int numCols = fieldMapping.getNumberOfDatastoreMappings();
                            for (int colNum = 0; colNum < numCols; colNum++) {
                                Column col = fieldMapping.getDatastoreMapping(colNum).getColumn();
                                if (col.getDefaultValue() == null && !col.isNullable()) {
                                    // Column needs to be nullable
                                    NucleusLogger.DATASTORE_SCHEMA.debug("Member " + mmd.getFullFieldName() + " uses superclass-table yet the field is not marked as nullable " + " nor does it have a default value, so setting the column as nullable");
                                    col.setNullable(true);
                                }
                            }
                        }
                        addMemberMapping(fieldMapping);
                    } else {
                        // Add the field to the appropriate secondary table
                        if (secondaryTables == null) {
                            secondaryTables = new HashMap();
                        }
                        SecondaryTable secTable = secondaryTables.get(mmd.getTable());
                        if (secTable == null) {
                            // Secondary table doesnt exist yet so create it to users specifications.
                            List<JoinMetaData> joinmds = theCmd.getJoinMetaData();
                            JoinMetaData theJoinMD = null;
                            if (joinmds != null) {
                                for (JoinMetaData joinmd : joinmds) {
                                    if (joinmd.getTable().equalsIgnoreCase(mmd.getTable()) && (joinmd.getCatalog() == null || (joinmd.getCatalog() != null && joinmd.getCatalog().equalsIgnoreCase(mmd.getCatalog()))) && (joinmd.getSchema() == null || (joinmd.getSchema() != null && joinmd.getSchema().equalsIgnoreCase(mmd.getSchema())))) {
                                        theJoinMD = joinmd;
                                        break;
                                    }
                                }
                            }
                            DatastoreIdentifier secTableIdentifier = storeMgr.getIdentifierFactory().newTableIdentifier(mmd.getTable());
                            // Use specified catalog, else take catalog of the owning table
                            String catalogName = mmd.getCatalog();
                            if (catalogName == null) {
                                catalogName = getCatalogName();
                            }
                            // Use specified schema, else take schema of the owning table
                            String schemaName = mmd.getSchema();
                            if (schemaName == null) {
                                schemaName = getSchemaName();
                            }
                            secTableIdentifier.setCatalogName(catalogName);
                            secTableIdentifier.setSchemaName(schemaName);
                            secTable = new SecondaryTable(secTableIdentifier, storeMgr, this, theJoinMD, clr);
                            secTable.preInitialize(clr);
                            secTable.initialize(clr);
                            secTable.postInitialize(clr);
                            secondaryTables.put(mmd.getTable(), secTable);
                        }
                        secTable.addMemberMapping(storeMgr.getMappingManager().getMapping(secTable, mmd, clr, FieldRole.ROLE_FIELD));
                    }
                } else if (mmd.getPersistenceModifier() != FieldPersistenceModifier.TRANSACTIONAL) {
                    throw new NucleusException(Localiser.msg("057006", mmd.getName())).setFatal();
                }
                // Calculate if we need a FK adding due to a 1-N (FK) relationship
                boolean needsFKToContainerOwner = false;
                RelationType relationType = mmd.getRelationType(clr);
                if (relationType == RelationType.ONE_TO_MANY_BI) {
                    AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
                    if (mmd.getJoinMetaData() == null && relatedMmds[0].getJoinMetaData() == null) {
                        needsFKToContainerOwner = true;
                    }
                } else if (relationType == RelationType.ONE_TO_MANY_UNI && !mmd.isSingleCollection()) {
                    if (mmd.getJoinMetaData() == null) {
                        needsFKToContainerOwner = true;
                    }
                }
                if (needsFKToContainerOwner) {
                    // 1-N uni/bidirectional using FK, so update the element side with a FK
                    if ((mmd.getCollection() != null && !SCOUtils.collectionHasSerialisedElements(mmd)) || (mmd.getArray() != null && !SCOUtils.arrayIsStoredInSingleColumn(mmd, storeMgr.getMetaDataManager()))) {
                        // 1-N ForeignKey collection/array, so add FK to element table
                        AbstractClassMetaData elementCmd = null;
                        if (mmd.hasCollection()) {
                            // Collection
                            elementCmd = storeMgr.getMetaDataManager().getMetaDataForClass(mmd.getCollection().getElementType(), clr);
                        } else {
                            // Array
                            elementCmd = storeMgr.getMetaDataManager().getMetaDataForClass(mmd.getType().getComponentType(), clr);
                        }
                        if (elementCmd == null) {
                            String[] implClassNames = storeMgr.getMetaDataManager().getClassesImplementingInterface(mmd.getCollection().getElementType(), clr);
                            if (implClassNames != null && implClassNames.length > 0) {
                                // Collection/array of interface type so apply callback to all implementation types
                                AbstractClassMetaData[] elementCmds = new AbstractClassMetaData[implClassNames.length];
                                for (int i = 0; i < implClassNames.length; i++) {
                                    elementCmds[i] = storeMgr.getMetaDataManager().getMetaDataForClass(implClassNames[i], clr);
                                }
                                // Run callbacks for each of the element classes.
                                for (int i = 0; i < elementCmds.length; i++) {
                                    storeMgr.addSchemaCallback(elementCmds[i].getFullClassName(), mmd);
                                    DatastoreClass dc = storeMgr.getDatastoreClass(elementCmds[i].getFullClassName(), clr);
                                    if (dc == null) {
                                        throw new NucleusException("Unable to add foreign-key to " + elementCmds[i].getFullClassName() + " to " + this + " since element has no table!");
                                    }
                                    if (dc instanceof ClassTable) {
                                        ClassTable ct = (ClassTable) dc;
                                        if (ct.isInitialized()) {
                                            // if the target table is already initialized, run the callbacks
                                            ct.runCallBacks(clr);
                                        }
                                    } else {
                                        NucleusLogger.DATASTORE_SCHEMA.info("Table " + toString() + " has to manage member " + mmd.getFullFieldName() + " yet the related element uses a VIEW so not remotely adding element FK owner column; assumed to be part of the VIEW definition");
                                    }
                                }
                            } else {
                                // Elements that are reference types or non-PC will come through here
                                if (mmd.hasCollection()) {
                                    NucleusLogger.METADATA.warn(Localiser.msg("057016", theCmd.getFullClassName(), mmd.getCollection().getElementType()));
                                } else {
                                    NucleusLogger.METADATA.warn(Localiser.msg("057014", theCmd.getFullClassName(), mmd.getType().getComponentType().getName()));
                                }
                            }
                        } else {
                            AbstractClassMetaData[] elementCmds = null;
                            // TODO : Cater for interface elements, and get the metadata for the implementation classes here
                            if (elementCmd.getBaseAbstractClassMetaData().getInheritanceMetaData().getStrategy() == InheritanceStrategy.COMPLETE_TABLE) {
                                // COMPLETE-TABLE inheritance in element, so really need FK in each!
                                Collection<String> elementSubclassNames = storeMgr.getSubClassesForClass(elementCmd.getFullClassName(), true, clr);
                                elementCmds = new ClassMetaData[elementSubclassNames != null ? 1 + elementSubclassNames.size() : 1];
                                int elemNo = 0;
                                elementCmds[elemNo++] = elementCmd;
                                if (elementSubclassNames != null) {
                                    for (String elementSubclassName : elementSubclassNames) {
                                        AbstractClassMetaData elemSubCmd = storeMgr.getMetaDataManager().getMetaDataForClass(elementSubclassName, clr);
                                        elementCmds[elemNo++] = elemSubCmd;
                                    }
                                }
                            } else if (elementCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
                                elementCmds = storeMgr.getClassesManagingTableForClass(elementCmd, clr);
                            } else {
                                elementCmds = new ClassMetaData[1];
                                elementCmds[0] = elementCmd;
                            }
                            ElementMetaData elemmd = mmd.getElementMetaData();
                            if (elemmd != null && !StringUtils.isWhitespace(elemmd.getTable())) {
                                DatastoreIdentifier requiredTableId = storeMgr.getIdentifierFactory().newTableIdentifier(elemmd.getTable());
                                DatastoreClass requiredTable = storeMgr.getDatastoreClass(requiredTableId);
                                if (requiredTable != null) {
                                    // TODO Respect specification of table in ElementMetaData rather than just defaulting to table of element type
                                    // Note that this will need updates to FKListStore, FKSetStore etc to look for the table
                                    NucleusLogger.GENERAL.warn("Member=" + mmd.getFullFieldName() + " has 1-N FK with required table=" + requiredTable + " : we don't currently support specification of the element table, and always take the default table for the element type");
                                /*for (int i=0;i<elementCmds.length;i++)
                                        {
                                            AbstractClassMetaData theElementCmd = elementCmds[i];
                                            while (theElementCmd != null)
                                            {
                                                if (requiredTable.managesClass(theElementCmd.getFullClassName()))
                                                {
                                                    if (theElementCmd != elementCmds[i])
                                                    {
                                                        elementCmds = new ClassMetaData[1];
                                                        elementCmds[0] = theElementCmd;
                                                        break;
                                                    }
                                                }
                                                theElementCmd = theElementCmd.getSuperAbstractClassMetaData();
                                            }
                                        }*/
                                } else {
                                    NucleusLogger.DATASTORE_SCHEMA.warn("Member " + mmd.getFullFieldName() + " specified element FK in table=" + elemmd.getTable() + " but table not known. Ignoring.");
                                }
                            }
                            // Run callbacks for each of the element classes
                            for (int i = 0; i < elementCmds.length; i++) {
                                storeMgr.addSchemaCallback(elementCmds[i].getFullClassName(), mmd);
                                DatastoreClass dc = storeMgr.getDatastoreClass(elementCmds[i].getFullClassName(), clr);
                                if (// If dc is null then we assume the (possible) element is abstract so no FK needed
                                dc != null) {
                                    if (dc instanceof ClassTable) {
                                        ClassTable ct = (ClassTable) dc;
                                        if (ct.isInitialized()) {
                                            // if the target table is already initialized, run the callbacks
                                            ct.runCallBacks(clr);
                                        }
                                    } else {
                                        NucleusLogger.DATASTORE_SCHEMA.info("Table " + toString() + " has to manage member " + mmd.getFullFieldName() + " yet the related element uses a VIEW so not remotely adding element FK owner column; assumed to be part of the VIEW definition");
                                    }
                                }
                            }
                        }
                    } else if (mmd.getMap() != null && !SCOUtils.mapHasSerialisedKeysAndValues(mmd)) {
                        // 1-N ForeignKey map, so add FK to value table
                        if (mmd.getKeyMetaData() != null && mmd.getKeyMetaData().getMappedBy() != null) {
                            // Key is stored in the value table so add the FK to the value table
                            AbstractClassMetaData valueCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getMap().getValueType(), clr);
                            if (valueCmd == null) {
                                // Interface elements will come through here and java.lang.String and others as well
                                NucleusLogger.METADATA.warn(Localiser.msg("057018", theCmd.getFullClassName(), mmd.getMap().getValueType()));
                            } else {
                                AbstractClassMetaData[] valueCmds = null;
                                // TODO : Cater for interface values, and get the metadata for the implementation classes here
                                if (valueCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
                                    valueCmds = storeMgr.getClassesManagingTableForClass(valueCmd, clr);
                                } else {
                                    valueCmds = new ClassMetaData[1];
                                    valueCmds[0] = valueCmd;
                                }
                                // Run callbacks for each of the value classes.
                                for (int i = 0; i < valueCmds.length; i++) {
                                    storeMgr.addSchemaCallback(valueCmds[i].getFullClassName(), mmd);
                                    DatastoreClass dc = storeMgr.getDatastoreClass(valueCmds[i].getFullClassName(), clr);
                                    if (dc instanceof ClassTable) {
                                        ClassTable ct = (ClassTable) dc;
                                        if (ct.isInitialized()) {
                                            // if the target table is already initialized, run the callbacks
                                            ct.runCallBacks(clr);
                                        }
                                    } else {
                                        NucleusLogger.DATASTORE_SCHEMA.info("Table " + toString() + " has to manage member " + mmd.getFullFieldName() + " yet the related value uses a VIEW so not remotely adding value owner FK column; assumed to be part of the VIEW definition");
                                    }
                                }
                            }
                        } else if (mmd.getValueMetaData() != null && mmd.getValueMetaData().getMappedBy() != null) {
                            // Value is stored in the key table so add the FK to the key table
                            AbstractClassMetaData keyCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getMap().getKeyType(), clr);
                            if (keyCmd == null) {
                                // Interface elements will come through here and java.lang.String and others as well
                                NucleusLogger.METADATA.warn(Localiser.msg("057019", theCmd.getFullClassName(), mmd.getMap().getKeyType()));
                            } else {
                                AbstractClassMetaData[] keyCmds = null;
                                // TODO : Cater for interface keys, and get the metadata for the implementation classes here
                                if (keyCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
                                    keyCmds = storeMgr.getClassesManagingTableForClass(keyCmd, clr);
                                } else {
                                    keyCmds = new ClassMetaData[1];
                                    keyCmds[0] = keyCmd;
                                }
                                // Run callbacks for each of the key classes.
                                for (int i = 0; i < keyCmds.length; i++) {
                                    storeMgr.addSchemaCallback(keyCmds[i].getFullClassName(), mmd);
                                    DatastoreClass dc = storeMgr.getDatastoreClass(keyCmds[i].getFullClassName(), clr);
                                    if (dc instanceof ClassTable) {
                                        ClassTable ct = (ClassTable) dc;
                                        if (ct.isInitialized()) {
                                            // if the target table is already initialized, run the callbacks
                                            ct.runCallBacks(clr);
                                        }
                                    } else {
                                        NucleusLogger.DATASTORE_SCHEMA.info("Table " + toString() + " has to manage member " + mmd.getFullFieldName() + " yet the related key uses a VIEW so not remotely adding key FK owner column; assumed to be part of the VIEW definition");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) HashMap(java.util.HashMap) ElementMetaData(org.datanucleus.metadata.ElementMetaData) MacroString(org.datanucleus.util.MacroString) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) RelationType(org.datanucleus.metadata.RelationType) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) JoinMetaData(org.datanucleus.metadata.JoinMetaData) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 54 with ColumnMetaData

use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-rdbms by datanucleus.

the class PersistableMapping method prepareDatastoreMapping.

/**
 * Method to prepare the PC mapping and add its associated datastore mappings.
 * @param clr The ClassLoaderResolver
 */
protected void prepareDatastoreMapping(ClassLoaderResolver clr) {
    if (roleForMember == FieldRole.ROLE_COLLECTION_ELEMENT) {
    // TODO Handle creation of columns in join table for collection of PCs
    } else if (roleForMember == FieldRole.ROLE_ARRAY_ELEMENT) {
    // TODO Handle creation of columns in join table for array of PCs
    } else if (roleForMember == FieldRole.ROLE_MAP_KEY) {
    // TODO Handle creation of columns in join table for map of PCs as keys
    } else if (roleForMember == FieldRole.ROLE_MAP_VALUE) {
    // TODO Handle creation of columns in join table for map of PCs as values
    } else {
        // Either one end of a 1-1 relation, or the N end of a N-1
        AbstractClassMetaData refCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getType(), clr);
        JavaTypeMapping referenceMapping = null;
        if (refCmd == null) {
            // User stupidity
            throw new NucleusUserException("You have a field " + mmd.getFullFieldName() + " that has type " + mmd.getTypeName() + " but this type has no known metadata. Your mapping is incorrect");
        }
        if (refCmd.getInheritanceMetaData() != null && refCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
            // 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) {
                    // TODO Only log this when it is really necessary. In some situations it is fine
                    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");
                }
            } else {
                // TODO Throw an exception ?
                return;
            }
            // TODO We need a mapping for each of the possible subclass tables
            referenceMapping = storeMgr.getDatastoreClass(cmds[0].getFullClassName(), clr).getIdMapping();
        } else if (refCmd.getInheritanceMetaData() != null && refCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.COMPLETE_TABLE) {
            // Find the other side of the relation
            DatastoreClass refTable = null;
            if (refCmd instanceof ClassMetaData && !((ClassMetaData) refCmd).isAbstract()) {
                refTable = storeMgr.getDatastoreClass(refCmd.getFullClassName(), clr);
            } else {
                Collection<String> refSubclasses = storeMgr.getSubClassesForClass(refCmd.getFullClassName(), true, clr);
                if (refSubclasses != null && !refSubclasses.isEmpty()) {
                    // if only 1 subclass then use that
                    String refSubclassName = refSubclasses.iterator().next();
                    refTable = storeMgr.getDatastoreClass(refSubclassName, clr);
                    if (refSubclasses.size() > 1) {
                        NucleusLogger.DATASTORE_SCHEMA.info("Field " + mmd.getFullFieldName() + " is a 1-1/N-1 relation and the other side had multiple possible classes " + "to which to create a foreign-key. Using first possible (" + refSubclassName + ")");
                    }
                }
            }
            if (refTable != null) {
                referenceMapping = refTable.getIdMapping();
            } else {
                throw new NucleusUserException("Field " + mmd.getFullFieldName() + " represents either a 1-1 relation, " + "or a N-1 relation where the other end uses \"complete-table\" inheritance strategy and either no table was found, or multiple possible tables!");
            }
        } else {
            // Default is to use the ID of the related object
            // TODO Add option to use a natural-id in the other class. Find the mapping using the targetColumnName
            referenceMapping = storeMgr.getDatastoreClass(mmd.getType().getName(), clr).getIdMapping();
        }
        // Generate a mapping from the columns of the referenced object to this mapping's ColumnMetaData
        CorrespondentColumnsMapper correspondentColumnsMapping = new CorrespondentColumnsMapper(mmd, table, referenceMapping, true);
        // Find any related field where this is part of a bidirectional relation
        RelationType relationType = mmd.getRelationType(clr);
        boolean createDatastoreMappings = true;
        if (relationType == RelationType.MANY_TO_ONE_BI) {
            AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
            // TODO Cater for more than 1 related field
            createDatastoreMappings = (relatedMmds[0].getJoinMetaData() == null);
        } else if (// TODO If join table then don't need this
        relationType == RelationType.ONE_TO_ONE_BI) {
            // Put the FK at the end without "mapped-by"
            createDatastoreMappings = (mmd.getMappedBy() == null);
        }
        if (mmd.getJoinMetaData() != null && (relationType == RelationType.MANY_TO_ONE_UNI || relationType == RelationType.ONE_TO_ONE_UNI || relationType == RelationType.ONE_TO_ONE_BI)) {
            if (relationType == RelationType.ONE_TO_ONE_UNI || relationType == RelationType.ONE_TO_ONE_BI) {
                throw new NucleusUserException("We do not currently support 1-1 relations via join table : " + mmd.getFullFieldName());
            }
            // create join table
            storeMgr.newJoinTable(table, mmd, clr);
        } else {
            // Loop through the datastore fields in the referenced class and create a datastore field for each
            for (int i = 0; i < referenceMapping.getNumberOfDatastoreMappings(); i++) {
                DatastoreMapping refDatastoreMapping = referenceMapping.getDatastoreMapping(i);
                JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(refDatastoreMapping.getJavaTypeMapping().getJavaType());
                this.addJavaTypeMapping(mapping);
                // Create physical datastore columns where we require a FK link to the related table.
                if (createDatastoreMappings) {
                    // Find the Column MetaData that maps to the referenced datastore field
                    ColumnMetaData colmd = correspondentColumnsMapping.getColumnMetaDataByIdentifier(refDatastoreMapping.getColumn().getIdentifier());
                    if (colmd == null) {
                        throw new NucleusUserException(Localiser.msg("041038", refDatastoreMapping.getColumn().getIdentifier(), toString())).setFatal();
                    }
                    // Create a Datastore field to equate to the referenced classes datastore field
                    MappingManager mmgr = storeMgr.getMappingManager();
                    Column col = mmgr.createColumn(mmd, table, mapping, colmd, refDatastoreMapping.getColumn(), clr);
                    // Add its datastore mapping
                    DatastoreMapping datastoreMapping = mmgr.createDatastoreMapping(mapping, col, refDatastoreMapping.getJavaTypeMapping().getJavaTypeForDatastoreMapping(i));
                    this.addDatastoreMapping(datastoreMapping);
                } else {
                    mapping.setReferenceMapping(referenceMapping);
                }
            }
        }
    }
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) DatastoreMapping(org.datanucleus.store.rdbms.mapping.datastore.DatastoreMapping) Column(org.datanucleus.store.rdbms.table.Column) RelationType(org.datanucleus.metadata.RelationType) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) MappingManager(org.datanucleus.store.rdbms.mapping.MappingManager) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) CorrespondentColumnsMapper(org.datanucleus.store.rdbms.mapping.CorrespondentColumnsMapper)

Example 55 with ColumnMetaData

use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-rdbms by datanucleus.

the class SerialisedElementPCMapping 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.getElementMetaData() != null && mmd.getElementMetaData().getColumnMetaData() != null && mmd.getElementMetaData().getColumnMetaData().length > 0) {
        colmd = mmd.getElementMetaData().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

ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)85 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)28 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)16 ClassMetaData (org.datanucleus.metadata.ClassMetaData)14 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)13 ElementMetaData (org.datanucleus.metadata.ElementMetaData)13 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)11 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)10 JoinMetaData (org.datanucleus.metadata.JoinMetaData)9 MetaDataManager (org.datanucleus.metadata.MetaDataManager)9 MappingManager (org.datanucleus.store.rdbms.mapping.MappingManager)8 Column (org.datanucleus.store.rdbms.table.Column)8 NucleusContext (org.datanucleus.NucleusContext)7 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)7 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)7 PrimaryKeyMetaData (org.datanucleus.metadata.PrimaryKeyMetaData)7 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)7 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)7 DiscriminatorMetaData (org.datanucleus.metadata.DiscriminatorMetaData)6 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)6