Java Code Examples for org.eclipse.persistence.internal.helper.DatabaseField#getColumnDefinition()

The following examples show how to use org.eclipse.persistence.internal.helper.DatabaseField#getColumnDefinition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JPAMDefaultTableGenerator.java    From jeddict with Apache License 2.0 4 votes vote down vote up
/**
 * Resolve the foreign key database field metadata in relation table or
 * direct collection/map table. Those metadata includes type, and maybe
 * dbtype/size/subsize if DatabaseField carries those info.
 */
protected DatabaseField resolveDatabaseField(DatabaseField childField, DatabaseField parentField) {
    //set through the type from the source table key field to the relation or direct collection table key field.
    DatabaseField resolvedDatabaseField = new DatabaseField();
    // find original field in the parent table, which contains actual type definitions
    // if 'resolvedParentField' is null, there is no corresponding field definition (typo?)
    DatabaseField resolvedParentField = databaseFields.get(parentField);

    resolvedDatabaseField.setName(childField.getName());
    //Table should be set, otherwise other same name field will be used wrongly because equals() is true.
    //Fix for GF#1392 the same name column for the entity and many-to-many table cause wrong pk constraint.
    resolvedDatabaseField.setTable(childField.getTable());

    // type definitions from parent field definition
    if (resolvedParentField != null) {
        resolvedDatabaseField.setType(resolvedParentField.getType());
        resolvedDatabaseField.setScale(resolvedParentField.getScale());
        resolvedDatabaseField.setLength(resolvedParentField.getLength());
        resolvedDatabaseField.setPrecision(resolvedParentField.getPrecision());
    }

    // these are defined in childField definition(see @JoinColumn)
    resolvedDatabaseField.setUnique(childField.isUnique());
    resolvedDatabaseField.setNullable(childField.isNullable());
    resolvedDatabaseField.setUpdatable(childField.isUpdatable());
    resolvedDatabaseField.setInsertable(childField.isInsertable());
    resolvedDatabaseField.setUseDelimiters(childField.shouldUseDelimiters());
    resolvedDatabaseField.useUpperCaseForComparisons(childField.getUseUpperCaseForComparisons());
    resolvedDatabaseField.setNameForComparisons(childField.getNameForComparisons());

    String columnDef = childField.getColumnDefinition();
    if (columnDef == null || columnDef.trim().equals("")) {
        // if childField has no column definition, follow the definition of the parent field
        if (resolvedParentField != null) {
            resolvedDatabaseField.setColumnDefinition(resolvedParentField.getColumnDefinition());
        }
    } else {
        resolvedDatabaseField.setColumnDefinition(columnDef);
    }

    return resolvedDatabaseField;
}
 
Example 2
Source File: JPAMDefaultTableGenerator.java    From jeddict with Apache License 2.0 4 votes vote down vote up
protected FieldDefinition getFieldDefFromDBField(Supplier<JPAMFieldDefinition> fieldDefSupplier, DatabaseField dbField) {
    FieldDefinition fieldDef = this.fieldMap.get(dbField);
    if (fieldDef == null) {
        fieldDef = fieldDefSupplier.get();
        fieldDef.setName(dbField.getNameDelimited(databasePlatform));

        //added for extending tables where the field needs to be looked up
        fieldDef.setDatabaseField(dbField);

        if (dbField.getColumnDefinition() != null && dbField.getColumnDefinition().length() > 0) {
            // This column definition would include the complete definition of the
            // column like type, size,  "NULL/NOT NULL" clause, unique key clause
            fieldDef.setTypeDefinition(dbField.getColumnDefinition());
        } else {
            Class fieldType = dbField.getType();
            FieldTypeDefinition fieldTypeDef = (fieldType == null) ? null : databasePlatform.getFieldTypeDefinition(fieldType);

            // Check if the user field is a String and only then allow the length specified
            // in the @Column annotation to be set on the field.
            if (fieldType != null) {
                // If a length has been specified, set it, otherwise let the
                // field def from individual platforms handle it.
                if (dbField.getLength() > 0) {
                    fieldDef.setSize(dbField.getLength());
                } else if (dbField.getPrecision() > 0) {
                    fieldDef.setSize(dbField.getPrecision());
                    fieldDef.setSubSize(dbField.getScale());
                }
            }

            if ((fieldType == null) || (!fieldType.isPrimitive() && (fieldTypeDef == null))) {
                //TODO: log a warning for inaccessible type or not convertable type.
                AbstractSessionLog.getLog().log(SessionLog.CONFIG, SessionLog.METADATA, "field_type_set_to_java_lang_string", dbField.getQualifiedName(), fieldType);

                //set the default type (lang.String) to all un-resolved java type, like null, Number, util.Date, NChar/NType, Calendar
                //sql.Blob/Clob, Object, or unknown type). Please refer to bug 4352820.
                fieldDef.setType(ClassConstants.STRING);
            } else {
                //need to convert the primitive type if applied.
                fieldDef.setType(ConversionManager.getObjectClass(fieldType));
            }

            fieldDef.setShouldAllowNull(dbField.isNullable());
            fieldDef.setUnique(dbField.isUnique());
        }
        this.fieldMap.put(dbField, fieldDef);
        this.databaseFields.put(dbField, dbField);
    }

    return fieldDef;
}