Search in sources :

Example 1 with MetaData

use of org.datanucleus.metadata.MetaData in project datanucleus-api-jdo by datanucleus.

the class JDOMetaDataHandlerTest method testParseDefaultNamespace.

public void testParseDefaultNamespace() {
    NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
    MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
    MetaData md = parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/package1.jdo"), "jdo");
    assertNotNull(md);
}
Also used : MetaData(org.datanucleus.metadata.MetaData) NucleusContext(org.datanucleus.NucleusContext) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) MetaDataParser(org.datanucleus.metadata.xml.MetaDataParser) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager)

Example 2 with MetaData

use of org.datanucleus.metadata.MetaData in project datanucleus-api-jdo by datanucleus.

the class PersistenceFileMetaDataHandlerTest method testParseNamespace.

public void testParseNamespace() {
    NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
    MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
    MetaData md = parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/persistence2.xml"), "persistence");
    assertNotNull(md);
}
Also used : MetaData(org.datanucleus.metadata.MetaData) NucleusContext(org.datanucleus.NucleusContext) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) MetaDataParser(org.datanucleus.metadata.xml.MetaDataParser) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager)

Example 3 with MetaData

use of org.datanucleus.metadata.MetaData 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 4 with MetaData

use of org.datanucleus.metadata.MetaData in project datanucleus-api-jdo by datanucleus.

the class JDOMetaDataHandlerTest method testParseNamespace.

public void testParseNamespace() {
    NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
    MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
    MetaData md = parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/package2.jdo"), "jdo");
    assertNotNull(md);
}
Also used : MetaData(org.datanucleus.metadata.MetaData) NucleusContext(org.datanucleus.NucleusContext) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) MetaDataParser(org.datanucleus.metadata.xml.MetaDataParser) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager)

Example 5 with MetaData

use of org.datanucleus.metadata.MetaData in project datanucleus-api-jdo by datanucleus.

the class PersistenceFileMetaDataHandlerTest method testParseDefaultNamespace.

public void testParseDefaultNamespace() {
    NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
    MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
    MetaData md = parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/persistence1.xml"), "persistence");
    assertNotNull(md);
}
Also used : MetaData(org.datanucleus.metadata.MetaData) NucleusContext(org.datanucleus.NucleusContext) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) MetaDataParser(org.datanucleus.metadata.xml.MetaDataParser) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager)

Aggregations

MetaData (org.datanucleus.metadata.MetaData)7 NucleusContext (org.datanucleus.NucleusContext)4 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)4 JDOMetaDataManager (org.datanucleus.api.jdo.metadata.JDOMetaDataManager)4 MetaDataParser (org.datanucleus.metadata.xml.MetaDataParser)4 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)2 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)2 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)2 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)2 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)2 DiscriminatorMetaData (org.datanucleus.metadata.DiscriminatorMetaData)2 VersionMetaData (org.datanucleus.metadata.VersionMetaData)2 AttributeConverter (javax.jdo.AttributeConverter)1 PersistenceNucleusContext (org.datanucleus.PersistenceNucleusContext)1 JDOTypeConverter (org.datanucleus.api.jdo.JDOTypeConverter)1 AbstractElementMetaData (org.datanucleus.metadata.AbstractElementMetaData)1 ArrayMetaData (org.datanucleus.metadata.ArrayMetaData)1 ClassMetaData (org.datanucleus.metadata.ClassMetaData)1 CollectionMetaData (org.datanucleus.metadata.CollectionMetaData)1 ElementMetaData (org.datanucleus.metadata.ElementMetaData)1