Search in sources :

Example 1 with MappingFieldElement

use of com.sun.jdo.api.persistence.model.mapping.MappingFieldElement in project Payara by payara.

the class DatabaseGenerator method generate.

/**
 * Generate database schema and mapping classes.  Iterate over all
 * persistence-capable classes, generating a table for each.  Within each
 * persistence-capable class, iterate over all fields and make columns
 * for each.  Then handle relationships separately, see {@link
 * #addRelationships}.
 * @return A DatabaseGenerator.Results instance which holds the results
 * of generation.
 */
private Results generate() throws DBException, ModelException {
    for (Iterator i = pcClasses.iterator(); i.hasNext(); ) {
        NameTuple nameTuple = (NameTuple) i.next();
        String pcClassName = nameTuple.getPersistenceClassName();
        String desiredTableName = nameTuple.getDesiredTableName();
        PersistenceClassElement pcClass = model.getPersistenceClass(pcClassName);
        String tableName = mappingPolicy.getTableName(desiredTableName, getShortClassName(nameTuple.getHashClassName()));
        TableElement table = DBElementFactory.createAndAttachTable(schema, tableName);
        UniqueKeyElement pKey = DBElementFactory.createAndAttachPrimaryKey(table, mappingPolicy.getPrimaryKeyConstraintName(table.getName().getName()));
        MappingClassElement mappingClass = createMappingClass(pcClass, table);
        PersistenceFieldElement[] fields = pcClass.getFields();
        if (fields != null) {
            for (int j = 0; j < fields.length; j++) {
                PersistenceFieldElement field = fields[j];
                String fieldName = field.getName();
                if (!(field instanceof RelationshipElement)) {
                    String columnName = mappingPolicy.getColumnName(desiredTableName, fieldName, tableName);
                    String fieldType = model.getFieldType(pcClassName, fieldName);
                    String fullFieldName = new StringBuilder(desiredTableName).append(DOT).append(fieldName).toString();
                    JDBCInfo columnType = DBElementFactory.getColumnType(fullFieldName, fieldType, mappingPolicy);
                    if (logger.isLoggable(Logger.FINEST)) {
                        logger.fine(// NOI18N
                        "DBGenerator.generate: " + tableName + "." + columnName + // NOI18N
                        ": " + columnType.toString());
                    }
                    ColumnElement column = DBElementFactory.createAndAttachColumn(columnName, table, columnType);
                    MappingFieldElement mappingField = createAndAttachMappingField(fieldName, mappingClass, column);
                    if (field.isKey()) {
                        column.setNullable(false);
                        pKey.addColumn(column);
                        pKey.getAssociatedIndex().addColumn(column);
                        mappingClass.getTable(tableName).addKeyColumn(column);
                    }
                }
            }
        }
        mappingClasses.put(pcClassName, mappingClass);
    }
    addRelationships();
    return new Results(schema, mappingClasses);
}
Also used : MappingFieldElement(com.sun.jdo.api.persistence.model.mapping.MappingFieldElement) PersistenceFieldElement(com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement) MappingClassElement(com.sun.jdo.api.persistence.model.mapping.MappingClassElement) PersistenceClassElement(com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement) MappingTableElement(com.sun.jdo.api.persistence.model.mapping.MappingTableElement) RelationshipElement(com.sun.jdo.api.persistence.model.jdo.RelationshipElement) MappingRelationshipElement(com.sun.jdo.api.persistence.model.mapping.MappingRelationshipElement)

Example 2 with MappingFieldElement

use of com.sun.jdo.api.persistence.model.mapping.MappingFieldElement in project Payara by payara.

the class DatabaseGenerator method createAndAttachMappingField.

/**
 * Create mapping field and add to mapping class
 * @param fieldName a String for field name
 * @param mappingClass mapping class object that field belong to
 * @return mapping field object
 * @throws ModelException
 */
private MappingFieldElement createAndAttachMappingField(String fieldName, MappingClassElement mappingClass, ColumnElement column) throws ModelException {
    MappingFieldElement mappingField = new MappingFieldElementImpl(fieldName, mappingClass);
    mappingClass.addField(mappingField);
    mappingField.addColumn(column);
    if (column.isBlobType()) {
        mappingField.setFetchGroup(MappingFieldElement.GROUP_NONE);
    } else {
        mappingField.setFetchGroup(MappingFieldElement.GROUP_DEFAULT);
    }
    return mappingField;
}
Also used : MappingFieldElement(com.sun.jdo.api.persistence.model.mapping.MappingFieldElement) MappingFieldElementImpl(com.sun.jdo.api.persistence.model.mapping.impl.MappingFieldElementImpl)

Example 3 with MappingFieldElement

use of com.sun.jdo.api.persistence.model.mapping.MappingFieldElement in project Payara by payara.

the class MappingGenerator method generateMappingClasses.

/**
 * Create mapping classes and schema based on database vendor name.
 * @param dbName a string for database vendor name
 * @param uniqueTableNames a Boolean to determin if use unique table names
 * during database generation
 * @param userPolicy a property object holding user overrides
 * @param inputFilesPath a directory where sun-cmp-mappings.xml is located
 * @throws IOException
 * @throws Schema2BeansException
 * @throws ModelException
 * @throws DBException
 * @throws ConversionException
 */
public DatabaseGenerator.Results generateMappingClasses(String dbName, Boolean uniqueTableNames, Properties userPolicy, String inputFilesPath) throws IOException, Schema2BeansException, ModelException, DBException, ConversionException {
    // generate mapping classes and dbschema in memory
    SunCmpMappings sunCmpMappings = null;
    // sun-cmp-mappings.xml does not exist, use DatabaseGenerator
    // to generate sun-cmp-mappings.xml, *.dbschema
    List pcClasses = new ArrayList();
    sunCmpMappings = getPartialSunCmpMappings(pcClasses, (uniqueTableNames != null) ? uniqueTableNames.booleanValue() : false);
    // load real jdo model and fake mapping model in memory
    ddHelper.setEnsureValidation(false);
    // create fake schema for partial mapping
    SchemaElement fakeSchema = new SchemaElement(new SchemaElementImpl());
    fakeSchema.setName(DBIdentifier.create(FAKE_NAME));
    // add newly created fake schema to SchemaElement cache
    SchemaElement.addToCache(fakeSchema);
    // pass null as class loader in order for MappingFile to load schema
    // from cache not from disk.
    loadMappingClasses(sunCmpMappings, null);
    DatabaseGenerator.Results results = generateSchema(pcClasses, dbName, uniqueTableNames, userPolicy);
    SchemaElement schema = results.getSchema();
    Set mappingClasses = results.getMappingClasses();
    // remove fake schema from cache since the correct schema is generated.
    SchemaElement.removeFromCache(FAKE_NAME);
    // clean up old version of schema in SchemaElement cache
    // if there is one
    SchemaElement.removeFromCache(schema.getName().getName());
    // add newly created schema to SchemaElement cache
    SchemaElement.addToCache(schema);
    // update mapping classes
    updateMappingClasses(mappingClasses);
    // model before returning the result.
    if (skipGeneratedFields) {
        Iterator iter = mappingClasses.iterator();
        while (iter.hasNext()) {
            MappingClassElement mapClassElt = (MappingClassElement) iter.next();
            if (mapClassElt != null) {
                String className = mapClassElt.getName();
                String ejbName = nameMapper.getEjbNameForPersistenceClass(className);
                PersistenceClassElement pce = (PersistenceClassElement) model.getPersistenceClass(className);
                PersistenceFieldElement[] allFields = pce.getFields();
                if (allFields != null) {
                    List generatedFieldList = new ArrayList();
                    // the generated fields from the model.
                    for (int i = 0; i < allFields.length; i++) {
                        PersistenceFieldElement pfe = allFields[i];
                        if (pfe != null) {
                            String pFieldName = pfe.getName();
                            String ejbFieldName = nameMapper.getEjbFieldForPersistenceField(className, pFieldName);
                            if (nameMapper.isGeneratedField(ejbName, ejbFieldName)) {
                                generatedFieldList.add(pfe);
                            }
                        }
                    }
                    // If the field is a version field, don't remove it
                    // from the model even though it is generated because
                    // it is needed to hold the version column information.
                    Iterator iterator = generatedFieldList.iterator();
                    while (iterator.hasNext()) {
                        PersistenceFieldElement pfe = (PersistenceFieldElement) iterator.next();
                        MappingFieldElement mfe = mapClassElt.getField(pfe.getName());
                        if (mfe != null && (!mfe.isVersion())) {
                            model.removeFieldElement(pfe);
                            mapClassElt.removeField(mfe);
                        }
                    }
                }
            }
        }
    }
    return results;
}
Also used : MappingFieldElement(com.sun.jdo.api.persistence.model.mapping.MappingFieldElement) PersistenceFieldElement(com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement) MappingClassElement(com.sun.jdo.api.persistence.model.mapping.MappingClassElement) DatabaseGenerator(com.sun.jdo.spi.persistence.generator.database.DatabaseGenerator) PersistenceClassElement(com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement) SchemaElementImpl(org.netbeans.modules.dbschema.jdbcimpl.SchemaElementImpl)

Example 4 with MappingFieldElement

use of com.sun.jdo.api.persistence.model.mapping.MappingFieldElement in project Payara by payara.

the class ClassDesc method initializeVersionFields.

/**
 * Initialize the list of field descriptors of version consistency fields.
 * The names of the version fields are obtained from
 * {@link MappingClassElement#getVersionFields}.
 */
private void initializeVersionFields() {
    int size = mdConfig.getVersionFields().size();
    Iterator versionFieldIterator = mdConfig.getVersionFields().iterator();
    versionFields = new LocalFieldDesc[size];
    for (int i = 0; i < size; i++) {
        MappingFieldElement mdField = (MappingFieldElement) versionFieldIterator.next();
        LocalFieldDesc f = (LocalFieldDesc) getField(mdField.getName());
        if (f != null) {
            if (logger.isLoggable()) {
                // NOI18N
                logger.finest("sqlstore.model.classdesc.vcfield", f.getName());
            }
            versionFields[i] = f;
            registerVersionFieldWithTable(f);
            // The fetch group for version fields should always be DFG.
            f.fetchGroup = FieldDesc.GROUP_DEFAULT;
            f.sqlProperties &= ~(FieldDesc.PROP_REF_INTEGRITY_UPDATES);
            f.sqlProperties |= FieldDesc.PROP_VERSION_FIELD;
        } else {
            throw new JDOFatalUserException(I18NHelper.getMessage(messages, // NOI18N
            "core.configuration.noneexistentvcfield", mdField.getName(), pcClass.getName()));
        }
    }
}
Also used : MappingFieldElement(com.sun.jdo.api.persistence.model.mapping.MappingFieldElement)

Aggregations

MappingFieldElement (com.sun.jdo.api.persistence.model.mapping.MappingFieldElement)4 PersistenceClassElement (com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement)2 PersistenceFieldElement (com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement)2 MappingClassElement (com.sun.jdo.api.persistence.model.mapping.MappingClassElement)2 RelationshipElement (com.sun.jdo.api.persistence.model.jdo.RelationshipElement)1 MappingRelationshipElement (com.sun.jdo.api.persistence.model.mapping.MappingRelationshipElement)1 MappingTableElement (com.sun.jdo.api.persistence.model.mapping.MappingTableElement)1 MappingFieldElementImpl (com.sun.jdo.api.persistence.model.mapping.impl.MappingFieldElementImpl)1 DatabaseGenerator (com.sun.jdo.spi.persistence.generator.database.DatabaseGenerator)1 SchemaElementImpl (org.netbeans.modules.dbschema.jdbcimpl.SchemaElementImpl)1