Search in sources :

Example 51 with NucleusUserException

use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-rdbms by datanucleus.

the class SumFunction method getExpression.

/* (non-Javadoc)
     * @see org.datanucleus.store.rdbms.sql.method.SQLMethod#getExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression, java.util.List)
     */
public SQLExpression getExpression(SQLStatement stmt, SQLExpression expr, List<SQLExpression> args) {
    if (expr == null) {
        if (args == null || args.size() != 1) {
            throw new NucleusException(getFunctionName() + " is only supported with a single argument");
        }
        // Use same java type as the argument
        SQLExpression argExpr = args.get(0);
        JavaTypeMapping m = null;
        Class cls = argExpr.getJavaTypeMapping().getJavaType();
        if (cls == Integer.class || cls == Short.class || cls == Long.class) {
            m = stmt.getSQLExpressionFactory().getMappingForType(Long.class, true);
        } else if (Number.class.isAssignableFrom(cls)) {
            m = stmt.getSQLExpressionFactory().getMappingForType(argExpr.getJavaTypeMapping().getJavaType(), true);
        } else {
            throw new NucleusUserException("Cannot perform static SUM with arg of type " + cls.getName());
        }
        return new AggregateNumericExpression(stmt, m, getFunctionName(), args);
    }
    throw new NucleusException(Localiser.msg("060002", getFunctionName(), expr));
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) AggregateNumericExpression(org.datanucleus.store.rdbms.sql.expression.AggregateNumericExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 52 with NucleusUserException

use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-rdbms by datanucleus.

the class ClassTable method initializeFKMapUniqueConstraints.

/**
 * Method to initialise unique constraints for 1-N Map using FK.
 * @param ownerMmd metadata for the field/property with the map in the owner class
 */
private void initializeFKMapUniqueConstraints(AbstractMemberMetaData ownerMmd) {
    // Load "mapped-by"
    AbstractMemberMetaData mfmd = null;
    // Field in our class that maps back to the owner class
    String map_field_name = ownerMmd.getMappedBy();
    if (map_field_name != null) {
        // Bidirectional
        mfmd = cmd.getMetaDataForMember(map_field_name);
        if (mfmd == null) {
            // Field not in primary class so may be in subclass so check all managed classes
            Iterator<AbstractClassMetaData> cmdIter = managedClassMetaData.iterator();
            while (cmdIter.hasNext()) {
                AbstractClassMetaData managedCmd = cmdIter.next();
                mfmd = managedCmd.getMetaDataForMember(map_field_name);
                if (mfmd != null) {
                    break;
                }
            }
        }
        if (mfmd == null) {
            // "mapped-by" refers to a field in our class that doesnt exist!
            throw new NucleusUserException(Localiser.msg("057036", map_field_name, cmd.getFullClassName(), ownerMmd.getFullFieldName()));
        }
        if (ownerMmd.getJoinMetaData() == null) {
            // Load field of key in value
            if (ownerMmd.getKeyMetaData() != null && ownerMmd.getKeyMetaData().getMappedBy() != null) {
                // Key field is stored in the value table
                AbstractMemberMetaData kmd = null;
                String key_field_name = ownerMmd.getKeyMetaData().getMappedBy();
                if (key_field_name != null) {
                    kmd = cmd.getMetaDataForMember(key_field_name);
                }
                if (kmd == null) {
                    // Field not in primary class so may be in subclass so check all managed classes
                    Iterator<AbstractClassMetaData> cmdIter = managedClassMetaData.iterator();
                    while (cmdIter.hasNext()) {
                        AbstractClassMetaData managedCmd = cmdIter.next();
                        kmd = managedCmd.getMetaDataForMember(key_field_name);
                        if (kmd != null) {
                            break;
                        }
                    }
                }
                if (kmd == null) {
                    throw new ClassDefinitionException(Localiser.msg("057007", mfmd.getFullFieldName(), key_field_name));
                }
                JavaTypeMapping ownerMapping = getMemberMapping(map_field_name);
                JavaTypeMapping keyMapping = getMemberMapping(kmd.getName());
                if (dba.supportsOption(DatastoreAdapter.NULLS_IN_CANDIDATE_KEYS) || (!ownerMapping.isNullable() && !keyMapping.isNullable())) {
                    // cannot do this so just omit it.
                    if (keyMapping.getTable() == this && ownerMapping.getTable() == this) {
                        CandidateKey ck = new CandidateKey(this, null);
                        // This HashSet is to avoid duplicate adding of columns.
                        HashSet addedColumns = new HashSet();
                        // Add columns for the owner field
                        int countOwnerFields = ownerMapping.getNumberOfDatastoreMappings();
                        for (int i = 0; i < countOwnerFields; i++) {
                            Column col = ownerMapping.getDatastoreMapping(i).getColumn();
                            addedColumns.add(col);
                            ck.addColumn(col);
                        }
                        // Add columns for the key field
                        int countKeyFields = keyMapping.getNumberOfDatastoreMappings();
                        for (int i = 0; i < countKeyFields; i++) {
                            Column col = keyMapping.getDatastoreMapping(i).getColumn();
                            if (!addedColumns.contains(col)) {
                                addedColumns.add(col);
                                ck.addColumn(col);
                            } else {
                                NucleusLogger.DATASTORE_SCHEMA.warn(Localiser.msg("057041", ownerMmd.getName()));
                            }
                        }
                        if (candidateKeysByMapField.put(mfmd, ck) != null) {
                            // We have multiple "mapped-by" coming to this field so give a warning that this may potentially
                            // cause problems. For example if they have the key field defined here for 2 different relations
                            // so you may get keys/values appearing in the other relation that shouldn't be.
                            // Logged as a WARNING for now.
                            // If there is a situation where this should throw an exception, please update this AND COMMENT WHY.
                            NucleusLogger.DATASTORE_SCHEMA.warn(Localiser.msg("057012", mfmd.getFullFieldName(), ownerMmd.getFullFieldName()));
                        }
                    }
                }
            } else if (ownerMmd.getValueMetaData() != null && ownerMmd.getValueMetaData().getMappedBy() != null) {
                // Value field is stored in the key table
                AbstractMemberMetaData vmd = null;
                String value_field_name = ownerMmd.getValueMetaData().getMappedBy();
                if (value_field_name != null) {
                    vmd = cmd.getMetaDataForMember(value_field_name);
                }
                if (vmd == null) {
                    throw new ClassDefinitionException(Localiser.msg("057008", mfmd));
                }
                JavaTypeMapping ownerMapping = getMemberMapping(map_field_name);
                JavaTypeMapping valueMapping = getMemberMapping(vmd.getName());
                if (dba.supportsOption(DatastoreAdapter.NULLS_IN_CANDIDATE_KEYS) || (!ownerMapping.isNullable() && !valueMapping.isNullable())) {
                    // cannot do this so just omit it.
                    if (valueMapping.getTable() == this && ownerMapping.getTable() == this) {
                        CandidateKey ck = new CandidateKey(this, null);
                        // This HashSet is to avoid duplicate adding of columns.
                        Set<Column> addedColumns = new HashSet<>();
                        // Add columns for the owner field
                        int countOwnerFields = ownerMapping.getNumberOfDatastoreMappings();
                        for (int i = 0; i < countOwnerFields; i++) {
                            Column col = ownerMapping.getDatastoreMapping(i).getColumn();
                            addedColumns.add(col);
                            ck.addColumn(col);
                        }
                        // Add columns for the value field
                        int countValueFields = valueMapping.getNumberOfDatastoreMappings();
                        for (int i = 0; i < countValueFields; i++) {
                            Column col = valueMapping.getDatastoreMapping(i).getColumn();
                            if (!addedColumns.contains(col)) {
                                addedColumns.add(col);
                                ck.addColumn(col);
                            } else {
                                NucleusLogger.DATASTORE_SCHEMA.warn(Localiser.msg("057042", ownerMmd.getName()));
                            }
                        }
                        if (candidateKeysByMapField.put(mfmd, ck) != null) {
                            // We have multiple "mapped-by" coming to this field so give a warning that this may potentially
                            // cause problems. For example if they have the key field defined here for 2 different relations
                            // so you may get keys/values appearing in the other relation that shouldn't be.
                            // Logged as a WARNING for now.
                            // If there is a situation where this should throw an exception, please update this AND COMMENT WHY.
                            NucleusLogger.DATASTORE_SCHEMA.warn(Localiser.msg("057012", mfmd.getFullFieldName(), ownerMmd.getFullFieldName()));
                        }
                    }
                }
            } else {
                // We can only have either the key stored in the value or the value stored in the key but nothing else!
                throw new ClassDefinitionException(Localiser.msg("057009", ownerMmd.getFullFieldName()));
            }
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) MacroString(org.datanucleus.util.MacroString) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) CandidateKey(org.datanucleus.store.rdbms.key.CandidateKey) ClassDefinitionException(org.datanucleus.store.rdbms.exceptions.ClassDefinitionException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) HashSet(java.util.HashSet)

Example 53 with NucleusUserException

use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-rdbms by datanucleus.

the class ElementContainerTable method initialize.

/**
 * Method to initialise the table definition. Adds the owner mapping.
 * @param clr The ClassLoaderResolver
 */
public void initialize(ClassLoaderResolver clr) {
    assertIsUninitialized();
    // Add owner mapping
    AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
    ColumnMetaData[] columnMetaData = null;
    if (mmd.getJoinMetaData() != null && mmd.getJoinMetaData().getColumnMetaData() != null && mmd.getJoinMetaData().getColumnMetaData().length > 0) {
        // Column mappings defined at this side (1-N, M-N)
        // When specified at this side they use the <join> tag
        columnMetaData = mmd.getJoinMetaData().getColumnMetaData();
    } else if (relatedMmds != null && relatedMmds[0].getElementMetaData() != null && relatedMmds[0].getElementMetaData().getColumnMetaData() != null && relatedMmds[0].getElementMetaData().getColumnMetaData().length > 0) {
        // Column mappings defined at other side (M-N)
        // When specified at other side they use the <element> tag
        // ** This is really only for Collections/Sets since M-N doesnt make sense for indexed Lists/arrays **
        columnMetaData = relatedMmds[0].getElementMetaData().getColumnMetaData();
    }
    try {
        ownerMapping = ColumnCreator.createColumnsForJoinTables(clr.classForName(ownerType), mmd, columnMetaData, storeMgr, this, false, false, FieldRole.ROLE_OWNER, clr, ownerTable);
    } catch (NoTableManagedException ntme) {
        // Maybe this is a join table from an embedded object, so no table to link back to
        throw new NucleusUserException("Table " + toString() + " for member=" + mmd.getFullFieldName() + " needs a column to link back to its owner, yet the owner type (" + ownerType + ") has no table of its own (embedded?)");
    }
    if (NucleusLogger.DATASTORE.isDebugEnabled()) {
        logMapping(mmd.getFullFieldName() + ".[OWNER]", ownerMapping);
    }
    // Add any distinguisher column
    if (mmd.hasExtension(MetaData.EXTENSION_MEMBER_RELATION_DISCRIM_COLUMN) || mmd.hasExtension(MetaData.EXTENSION_MEMBER_RELATION_DISCRIM_VALUE)) {
        // Generate some columnMetaData for our new column
        String colName = mmd.getValueForExtension(MetaData.EXTENSION_MEMBER_RELATION_DISCRIM_COLUMN);
        if (colName == null) {
            // No column defined so use a fallback name
            colName = "RELATION_DISCRIM";
        }
        ColumnMetaData colmd = new ColumnMetaData();
        colmd.setName(colName);
        boolean relationDiscriminatorPk = false;
        if (mmd.hasExtension(MetaData.EXTENSION_MEMBER_RELATION_DISCRIM_PK) && mmd.getValueForExtension(MetaData.EXTENSION_MEMBER_RELATION_DISCRIM_PK).equalsIgnoreCase("true")) {
            // Default this to not be part of the PK of the join table, but allow the user to override it
            relationDiscriminatorPk = true;
        }
        if (!relationDiscriminatorPk) {
            // Allow for elements not in any discriminated collection (when not PK)
            colmd.setAllowsNull(Boolean.TRUE);
        }
        // Create the mapping and its datastore column (only support String relation discriminators here)
        relationDiscriminatorMapping = storeMgr.getMappingManager().getMapping(String.class);
        ColumnCreator.createIndexColumn(relationDiscriminatorMapping, storeMgr, clr, this, colmd, relationDiscriminatorPk);
        relationDiscriminatorValue = mmd.getValueForExtension(MetaData.EXTENSION_MEMBER_RELATION_DISCRIM_VALUE);
        if (relationDiscriminatorValue == null) {
            // No value defined so just use the field name
            relationDiscriminatorValue = mmd.getFullFieldName();
        }
    }
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) NoTableManagedException(org.datanucleus.store.rdbms.exceptions.NoTableManagedException)

Example 54 with NucleusUserException

use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-rdbms by datanucleus.

the class MapTable method initialize.

/**
 * Method to initialise the table definition.
 * @param clr The ClassLoaderResolver
 */
public void initialize(ClassLoaderResolver clr) {
    assertIsUninitialized();
    MapMetaData mapmd = mmd.getMap();
    if (mapmd == null) {
        throw new NucleusUserException(Localiser.msg("057017", mmd));
    }
    PrimaryKeyMetaData pkmd = (mmd.getJoinMetaData() != null ? mmd.getJoinMetaData().getPrimaryKeyMetaData() : null);
    boolean pkColsSpecified = (pkmd != null && pkmd.getColumnMetaData() != null);
    boolean pkRequired = requiresPrimaryKey();
    // Add owner mapping
    ColumnMetaData[] ownerColmd = null;
    if (mmd.getJoinMetaData() != null && mmd.getJoinMetaData().getColumnMetaData() != null && mmd.getJoinMetaData().getColumnMetaData().length > 0) {
        // Column mappings defined at this side (1-N, M-N)
        // When specified at this side they use the <join> tag
        ownerColmd = mmd.getJoinMetaData().getColumnMetaData();
    }
    ownerMapping = ColumnCreator.createColumnsForJoinTables(clr.classForName(ownerType), mmd, ownerColmd, storeMgr, this, pkRequired, false, FieldRole.ROLE_OWNER, clr, null);
    if (NucleusLogger.DATASTORE.isDebugEnabled()) {
        logMapping(mmd.getFullFieldName() + ".[OWNER]", ownerMapping);
    }
    String keyValueFieldName = (mmd.getKeyMetaData() != null ? mmd.getKeyMetaData().getMappedBy() : null);
    String valueKeyFieldName = (mmd.getValueMetaData() != null ? mmd.getValueMetaData().getMappedBy() : null);
    // Add key mapping
    boolean keyPC = (mmd.hasMap() && mmd.getMap().keyIsPersistent());
    Class keyCls = clr.classForName(mapmd.getKeyType());
    if (keyValueFieldName != null && isEmbeddedValuePC()) {
    // Added in value code
    } else if (isSerialisedKey() || isEmbeddedKeyPC() || (isEmbeddedKey() && !keyPC) || ClassUtils.isReferenceType(keyCls)) {
        // Key = PC(embedded), PC(serialised), Non-PC(serialised), Non-PC(embedded), Reference
        keyMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_MAP_KEY);
        if (Boolean.TRUE.equals(mmd.getContainer().allowNulls())) {
            // Make all key col(s) nullable so we can store null elements
            for (int i = 0; i < keyMapping.getNumberOfDatastoreMappings(); i++) {
                Column elementCol = keyMapping.getDatastoreMapping(i).getColumn();
                elementCol.setNullable(true);
            }
        }
        if (NucleusLogger.DATASTORE.isDebugEnabled()) {
            logMapping(mmd.getFullFieldName() + ".[KEY]", keyMapping);
        }
        if (valueKeyFieldName != null && isEmbeddedKeyPC()) {
            // Key (PC) is embedded and value is a field of the key
            EmbeddedKeyPCMapping embMapping = (EmbeddedKeyPCMapping) keyMapping;
            valueMapping = embMapping.getJavaTypeMapping(valueKeyFieldName);
        }
    } else {
        // Key = PC
        ColumnMetaData[] keyColmd = 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)
            keyColmd = keymd.getColumnMetaData();
        }
        keyMapping = ColumnCreator.createColumnsForJoinTables(keyCls, mmd, keyColmd, storeMgr, this, false, false, FieldRole.ROLE_MAP_KEY, clr, null);
        if (mmd.getContainer().allowNulls() == Boolean.TRUE) {
            // Make all key col(s) nullable so we can store null elements
            for (int i = 0; i < keyMapping.getNumberOfDatastoreMappings(); i++) {
                Column elementCol = keyMapping.getDatastoreMapping(i).getColumn();
                elementCol.setNullable(true);
            }
        }
        if (NucleusLogger.DATASTORE.isDebugEnabled()) {
            logMapping(mmd.getFullFieldName() + ".[KEY]", keyMapping);
        }
    }
    // Add value mapping
    boolean valuePC = (mmd.hasMap() && mmd.getMap().valueIsPersistent());
    Class valueCls = clr.classForName(mapmd.getValueType());
    if (valueKeyFieldName != null && isEmbeddedKeyPC()) {
    // Added in key code
    } else if (isSerialisedValue() || isEmbeddedValuePC() || (isEmbeddedValue() && !valuePC) || ClassUtils.isReferenceType(valueCls)) {
        // Value = PC(embedded), PC(serialised), Non-PC(serialised), Non-PC(embedded), Reference
        valueMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_MAP_VALUE);
        if (mmd.getContainer().allowNulls() == Boolean.TRUE) {
            // Make all value col(s) nullable so we can store null elements
            for (int i = 0; i < valueMapping.getNumberOfDatastoreMappings(); i++) {
                Column elementCol = valueMapping.getDatastoreMapping(i).getColumn();
                elementCol.setNullable(true);
            }
        }
        if (NucleusLogger.DATASTORE.isDebugEnabled()) {
            logMapping(mmd.getFullFieldName() + ".[VALUE]", valueMapping);
        }
        if (keyValueFieldName != null && isEmbeddedValuePC()) {
            // Value (PC) is embedded and key is a field of the value
            EmbeddedValuePCMapping embMapping = (EmbeddedValuePCMapping) valueMapping;
            keyMapping = embMapping.getJavaTypeMapping(keyValueFieldName);
        }
    } else {
        // Value = PC
        ColumnMetaData[] valueColmd = 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)
            valueColmd = valuemd.getColumnMetaData();
        }
        valueMapping = ColumnCreator.createColumnsForJoinTables(clr.classForName(mapmd.getValueType()), mmd, valueColmd, storeMgr, this, false, true, FieldRole.ROLE_MAP_VALUE, clr, null);
        if (mmd.getContainer().allowNulls() == Boolean.TRUE) {
            // Make all value col(s) nullable so we can store null elements
            for (int i = 0; i < valueMapping.getNumberOfDatastoreMappings(); i++) {
                Column elementCol = valueMapping.getDatastoreMapping(i).getColumn();
                elementCol.setNullable(true);
            }
        }
        if (NucleusLogger.DATASTORE.isDebugEnabled()) {
            logMapping(mmd.getFullFieldName() + ".[VALUE]", valueMapping);
        }
    }
    // Add order mapping if required
    boolean orderRequired = false;
    if (mmd.getOrderMetaData() != null) {
        // User requested order column so add one
        orderRequired = true;
    } else if (requiresPrimaryKey() && !pkColsSpecified) {
        // PK is required so maybe need to add an index to form the PK
        if (isEmbeddedKeyPC()) {
            if (mmd.hasExtension("surrogate-pk-column") && mmd.getValueForExtension("surrogate-pk-column").equalsIgnoreCase("true")) {
                // Allow user to request surrogate pk column be added (for use with JPA)
                orderRequired = true;
            } else if (storeMgr.getApiAdapter().getName().equalsIgnoreCase("JDO") && mmd.getMap().getKeyClassMetaData(clr).getIdentityType() != IdentityType.APPLICATION) {
                // Embedded key PC with datastore id so we need an index to form the PK TODO It is arguable that we can just use all embedded key fields as part of PK here always
                orderRequired = true;
            }
        } else if (isSerialisedKey()) {
            // Serialised key, so need an index to form the PK
            orderRequired = true;
        } else if (keyMapping instanceof ReferenceMapping) {
            // ReferenceMapping, so have order if more than 1 implementation
            ReferenceMapping refMapping = (ReferenceMapping) keyMapping;
            if (refMapping.getJavaTypeMapping().length > 1) {
                orderRequired = true;
            }
        } else if (!(keyMapping instanceof PersistableMapping)) {
            // Non-PC, so depends if the key column can be used as part of a PK
            // TODO This assumes the keyMapping has a single column but what if it is Color with 4 cols?
            Column elementCol = keyMapping.getDatastoreMapping(0).getColumn();
            if (!storeMgr.getDatastoreAdapter().isValidPrimaryKeyType(elementCol.getJdbcType())) {
                // Not possible to use this Non-PC type as part of the PK
                orderRequired = true;
            }
        }
    }
    if (orderRequired) {
        // Order/Adapter (index) column is required (integer based)
        ColumnMetaData orderColmd = null;
        if (mmd.getOrderMetaData() != null && mmd.getOrderMetaData().getColumnMetaData() != null && mmd.getOrderMetaData().getColumnMetaData().length > 0) {
            // Specified "order" column info
            orderColmd = mmd.getOrderMetaData().getColumnMetaData()[0];
            if (orderColmd.getName() == null) {
                // No column name so use default
                orderColmd = new ColumnMetaData(orderColmd);
                DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
                orderColmd.setName(id.getName());
            }
        } else {
            // No column name so use default
            DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
            orderColmd = new ColumnMetaData();
            orderColmd.setName(id.getName());
        }
        // JDO2 spec [18.5] order column is assumed to be "int"
        orderMapping = storeMgr.getMappingManager().getMapping(int.class);
        ColumnCreator.createIndexColumn(orderMapping, storeMgr, clr, this, orderColmd, pkRequired && !pkColsSpecified);
        if (NucleusLogger.DATASTORE.isDebugEnabled()) {
            logMapping(mmd.getFullFieldName() + ".[ORDER]", orderMapping);
        }
    }
    // Define primary key of the join table (if any)
    if (pkRequired) {
        if (pkColsSpecified) {
            // Apply the users PK specification
            applyUserPrimaryKeySpecification(pkmd);
        } else {
            // Define PK using internal rules
            if (orderRequired) {
                // Order column specified so owner+order are the PK
                orderMapping.getDatastoreMapping(0).getColumn().setPrimaryKey();
            } else {
                // No order column specified so owner+key are the PK
                for (int i = 0; i < keyMapping.getNumberOfDatastoreMappings(); i++) {
                    keyMapping.getDatastoreMapping(i).getColumn().setPrimaryKey();
                }
            }
        }
    }
    if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
        NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("057023", this));
    }
    storeMgr.registerTableInitialized(this);
    state = TABLE_STATE_INITIALIZED;
}
Also used : KeyMetaData(org.datanucleus.metadata.KeyMetaData) PrimaryKeyMetaData(org.datanucleus.metadata.PrimaryKeyMetaData) ForeignKeyMetaData(org.datanucleus.metadata.ForeignKeyMetaData) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) EmbeddedValuePCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedValuePCMapping) MapMetaData(org.datanucleus.metadata.MapMetaData) PrimaryKeyMetaData(org.datanucleus.metadata.PrimaryKeyMetaData) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) ReferenceMapping(org.datanucleus.store.rdbms.mapping.java.ReferenceMapping) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) ValueMetaData(org.datanucleus.metadata.ValueMetaData) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) EmbeddedKeyPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping)

Example 55 with NucleusUserException

use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-rdbms by datanucleus.

the class PersistableJoinTable method getExpectedCandidateKeys.

/**
 * Accessor for the candidate keys for this table.
 * @return The indices
 */
protected List getExpectedCandidateKeys() {
    // The indices required by foreign keys (BaseTable)
    List candidateKeys = super.getExpectedCandidateKeys();
    if (mmd.getJoinMetaData() != null && mmd.getJoinMetaData().getUniqueMetaData() != null) {
        // User has defined a unique key on the join table
        UniqueMetaData unimd = mmd.getJoinMetaData().getUniqueMetaData();
        if (unimd.getNumberOfColumns() > 0) {
            String[] columnNames = unimd.getColumnNames();
            CandidateKey uniKey = new CandidateKey(this, null);
            IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
            for (String columnName : columnNames) {
                Column col = getColumn(idFactory.newColumnIdentifier(columnName));
                if (col != null) {
                    uniKey.addColumn(col);
                } else {
                    throw new NucleusUserException("Unique key on join-table " + this + " has column " + columnName + " that is not found");
                }
            }
            candidateKeys.add(uniKey);
        }
    }
    return candidateKeys;
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ArrayList(java.util.ArrayList) List(java.util.List) UniqueMetaData(org.datanucleus.metadata.UniqueMetaData) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) CandidateKey(org.datanucleus.store.rdbms.key.CandidateKey)

Aggregations

NucleusUserException (org.datanucleus.exceptions.NucleusUserException)258 NucleusException (org.datanucleus.exceptions.NucleusException)65 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)51 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)46 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)46 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)41 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)36 ArrayList (java.util.ArrayList)34 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)30 ObjectProvider (org.datanucleus.state.ObjectProvider)30 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)26 Expression (org.datanucleus.query.expression.Expression)24 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)23 SQLException (java.sql.SQLException)22 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)21 NullLiteral (org.datanucleus.store.rdbms.sql.expression.NullLiteral)21 SQLLiteral (org.datanucleus.store.rdbms.sql.expression.SQLLiteral)21 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)20 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)20 BigInteger (java.math.BigInteger)19