Example usage for org.hibernate.cfg NamingStrategy propertyToColumnName

List of usage examples for org.hibernate.cfg NamingStrategy propertyToColumnName

Introduction

In this page you can find the example usage for org.hibernate.cfg NamingStrategy propertyToColumnName.

Prototype

public String propertyToColumnName(String propertyName);

Source Link

Document

Return a column name for a property path expression

Usage

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

protected void bindCollectionWithJoinTable(GrailsDomainClassProperty property, Mappings mappings,
        Collection collection, PropertyConfig config, String sessionFactoryBeanName) {

    NamingStrategy namingStrategy = getNamingStrategy(sessionFactoryBeanName);

    SimpleValue element;//from  w  w  w .j  a v  a  2  s.co m
    if (property.isBasicCollectionType()) {
        element = new SimpleValue(mappings, collection.getCollectionTable());
    } else {
        // for a normal unidirectional one-to-many we use a join column
        element = new ManyToOne(mappings, collection.getCollectionTable());
        bindUnidirectionalOneToManyInverseValues(property, (ManyToOne) element);
    }
    collection.setInverse(false);

    String columnName;

    final boolean hasJoinColumnMapping = hasJoinColumnMapping(config);
    if (property.isBasicCollectionType()) {
        final Class<?> referencedType = property.getReferencedPropertyType();
        String className = referencedType.getName();
        final boolean isEnum = referencedType.isEnum();
        if (hasJoinColumnMapping) {
            columnName = config.getJoinTable().getColumn().getName();
        } else {
            columnName = isEnum ? namingStrategy.propertyToColumnName(className)
                    : addUnderscore(namingStrategy.propertyToColumnName(property.getName()),
                            namingStrategy.propertyToColumnName(className));
        }

        if (isEnum) {
            bindEnumType(property, referencedType, element, columnName);
        } else {

            String typeName = getTypeName(property, config, getMapping(property.getDomainClass()));
            if (typeName == null) {
                Type type = mappings.getTypeResolver().basic(className);
                if (type != null) {
                    typeName = type.getName();
                }
            }
            if (typeName == null) {
                String domainName = property.getDomainClass().getName();
                throw new MappingException("Missing type or column for column[" + columnName + "] on domain["
                        + domainName + "] referencing[" + className + "]");
            }

            bindSimpleValue(typeName, element, true, columnName, mappings);
            if (hasJoinColumnMapping) {
                bindColumnConfigToColumn(getColumnForSimpleValue(element), config.getJoinTable().getColumn());
            }
        }
    } else {
        final GrailsDomainClass domainClass = property.getReferencedDomainClass();

        Mapping m = getMapping(domainClass.getClazz());
        if (hasCompositeIdentifier(m)) {
            CompositeIdentity ci = (CompositeIdentity) m.getIdentity();
            bindCompositeIdentifierToManyToOne(property, element, ci, domainClass, EMPTY_PATH,
                    sessionFactoryBeanName);
        } else {
            if (hasJoinColumnMapping) {
                columnName = config.getJoinTable().getColumn().getName();
            } else {
                columnName = namingStrategy.propertyToColumnName(domainClass.getPropertyName())
                        + FOREIGN_KEY_SUFFIX;
            }

            bindSimpleValue("long", element, true, columnName, mappings);
        }
    }

    collection.setElement(element);

    bindCollectionForPropertyConfig(collection, config);
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

/**
 * Calculates the mapping table for a many-to-many. One side of
 * the relationship has to "own" the relationship so that there is not a situation
 * where you have two mapping tables for left_right and right_left
 *//*from  ww  w .j  a v  a2  s  .  c om*/
protected String calculateTableForMany(GrailsDomainClassProperty property, String sessionFactoryBeanName) {
    NamingStrategy namingStrategy = getNamingStrategy(sessionFactoryBeanName);

    String propertyColumnName = namingStrategy.propertyToColumnName(property.getName());
    //fix for GRAILS-5895
    PropertyConfig config = getPropertyConfig(property);
    JoinTable jt = config != null ? config.getJoinTable() : null;
    boolean hasJoinTableMapping = jt != null && jt.getName() != null;
    String left = getTableName(property.getDomainClass(), sessionFactoryBeanName);

    if (Map.class.isAssignableFrom(property.getType())) {
        if (hasJoinTableMapping) {
            return jt.getName();
        }
        return addUnderscore(left, propertyColumnName);
    }

    if (property.isBasicCollectionType()) {
        if (hasJoinTableMapping) {
            return jt.getName();
        }
        return addUnderscore(left, propertyColumnName);
    }

    String right = getTableName(property.getReferencedDomainClass(), sessionFactoryBeanName);

    if (property.isManyToMany()) {
        if (hasJoinTableMapping) {
            return jt.getName();
        }
        if (property.isOwningSide()) {
            return addUnderscore(left, propertyColumnName);
        }
        return addUnderscore(right, namingStrategy.propertyToColumnName(property.getOtherSide().getName()));
    }

    if (shouldCollectionBindWithJoinColumn(property)) {
        if (hasJoinTableMapping) {
            return jt.getName();
        }
        left = trimBackTigs(left);
        right = trimBackTigs(right);
        return addUnderscore(left, right);
    }

    if (property.isOwningSide()) {
        return addUnderscore(left, right);
    }
    return addUnderscore(right, left);
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

/**
 * Binds a many-to-one relationship to the
 *
 *///w w w  . ja  va  2 s.com
@SuppressWarnings("unchecked")
protected void bindManyToOne(GrailsDomainClassProperty property, ManyToOne manyToOne, String path,
        Mappings mappings, String sessionFactoryBeanName) {

    NamingStrategy namingStrategy = getNamingStrategy(sessionFactoryBeanName);

    bindManyToOneValues(property, manyToOne);
    GrailsDomainClass refDomainClass = property.isManyToMany() ? property.getDomainClass()
            : property.getReferencedDomainClass();
    Mapping mapping = getMapping(refDomainClass);
    boolean isComposite = hasCompositeIdentifier(mapping);
    if (isComposite) {
        CompositeIdentity ci = (CompositeIdentity) mapping.getIdentity();
        bindCompositeIdentifierToManyToOne(property, manyToOne, ci, refDomainClass, path,
                sessionFactoryBeanName);
    } else {
        if (property.isCircular() && property.isManyToMany()) {
            PropertyConfig pc = getPropertyConfig(property);

            if (pc == null) {
                if (mapping == null) {
                    mapping = new Mapping();
                    MAPPING_CACHE.put(refDomainClass.getClazz(), mapping);
                }
                pc = new PropertyConfig();
                mapping.getColumns().put(property.getName(), pc);
            }
            if (!hasJoinKeyMapping(pc)) {
                JoinTable jt = new JoinTable();
                final ColumnConfig columnConfig = new ColumnConfig();
                columnConfig.setName(namingStrategy.propertyToColumnName(property.getName()) + UNDERSCORE
                        + FOREIGN_KEY_SUFFIX);
                jt.setKey(columnConfig);
                pc.setJoinTable(jt);
            }
            bindSimpleValue(property, manyToOne, path, pc, sessionFactoryBeanName);
        } else {
            // bind column
            bindSimpleValue(property, null, manyToOne, path, mappings, sessionFactoryBeanName);
        }
    }

    PropertyConfig config = getPropertyConfig(property);
    if (property.isOneToOne() && !isComposite) {
        manyToOne.setAlternateUniqueKey(true);
        Column c = getColumnForSimpleValue(manyToOne);
        if (config != null) {
            c.setUnique(config.isUnique());
        } else if (property.isBidirectional() && property.getOtherSide().isHasOne()) {
            c.setUnique(true);
        }
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

protected String getColumnNameForPropertyAndPath(GrailsDomainClassProperty grailsProp, String path,
        ColumnConfig cc, String sessionFactoryBeanName) {

    NamingStrategy namingStrategy = getNamingStrategy(sessionFactoryBeanName);

    // First try the column config.
    String columnName = null;//from   w  ww  .  j a v  a  2 s .  c o m
    if (cc == null) {
        // No column config given, so try to fetch it from the mapping
        GrailsDomainClass domainClass = grailsProp.getDomainClass();
        Mapping m = getMapping(domainClass.getClazz());
        if (m != null) {
            PropertyConfig c = m.getPropertyConfig(grailsProp.getName());

            if (supportsJoinColumnMapping(grailsProp) && hasJoinKeyMapping(c)) {
                columnName = c.getJoinTable().getKey().getName();
            } else if (c != null && c.getColumn() != null) {
                columnName = c.getColumn();
            }
        }
    } else {
        if (supportsJoinColumnMapping(grailsProp)) {
            PropertyConfig pc = getPropertyConfig(grailsProp);
            if (hasJoinKeyMapping(pc)) {
                columnName = pc.getJoinTable().getKey().getName();
            } else {
                columnName = cc.getName();
            }
        } else {
            columnName = cc.getName();
        }
    }

    if (columnName == null) {
        if (isNotEmpty(path)) {
            columnName = addUnderscore(namingStrategy.propertyToColumnName(path),
                    getDefaultColumnName(grailsProp, sessionFactoryBeanName));
        } else {
            columnName = getDefaultColumnName(grailsProp, sessionFactoryBeanName);
        }
    }
    return columnName;
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

protected String getDefaultColumnName(GrailsDomainClassProperty property, String sessionFactoryBeanName) {

    NamingStrategy namingStrategy = getNamingStrategy(sessionFactoryBeanName);

    String columnName = namingStrategy.propertyToColumnName(property.getName());
    if (property.isAssociation() && property.getReferencedDomainClass() != null) {

        if (property.isManyToMany()) {
            return getForeignKeyForPropertyDomainClass(property, sessionFactoryBeanName);
        }//from   w ww  .  java 2 s.  co  m

        if (!property.isBidirectional() && property.isOneToMany()) {
            String prefix = namingStrategy.classToTableName(property.getDomainClass().getName());
            return addUnderscore(prefix, columnName) + FOREIGN_KEY_SUFFIX;
        }

        if (property.isInherited() && isBidirectionalManyToOne(property)) {
            return namingStrategy.propertyToColumnName(property.getDomainClass().getName()) + '_' + columnName
                    + FOREIGN_KEY_SUFFIX;
        }

        return columnName + FOREIGN_KEY_SUFFIX;
    }

    if (property.isBasicCollectionType()) {
        return getForeignKeyForPropertyDomainClass(property, sessionFactoryBeanName);
    }

    return columnName;
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

protected String getForeignKeyForPropertyDomainClass(GrailsDomainClassProperty property,
        String sessionFactoryBeanName) {
    final String propertyName = property.getDomainClass().getPropertyName();
    NamingStrategy namingStrategy = getNamingStrategy(sessionFactoryBeanName);
    return namingStrategy.propertyToColumnName(propertyName) + FOREIGN_KEY_SUFFIX;
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

protected String getIndexColumnName(GrailsDomainClassProperty property, String sessionFactoryBeanName) {
    PropertyConfig pc = getPropertyConfig(property);
    if (pc != null && pc.getIndexColumn() != null && pc.getIndexColumn().getColumn() != null) {
        return pc.getIndexColumn().getColumn();
    }/*from w w w  . j a  va 2s.c  o m*/
    NamingStrategy namingStrategy = getNamingStrategy(sessionFactoryBeanName);
    return namingStrategy.propertyToColumnName(property.getName()) + UNDERSCORE
            + IndexedCollection.DEFAULT_INDEX_COLUMN_NAME;
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

protected String getMapElementName(GrailsDomainClassProperty property, String sessionFactoryBeanName) {
    PropertyConfig pc = getPropertyConfig(property);

    if (hasJoinTableColumnNameMapping(pc)) {
        return pc.getJoinTable().getColumn().getName();
    }/*from w ww  .j a  va2  s.  c o  m*/

    NamingStrategy namingStrategy = getNamingStrategy(sessionFactoryBeanName);
    return namingStrategy.propertyToColumnName(property.getName()) + UNDERSCORE
            + IndexedCollection.DEFAULT_ELEMENT_COLUMN_NAME;
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java

License:Apache License

private static void bindCollectionWithJoinTable(GrailsDomainClassProperty property, Mappings mappings,
        Collection collection, PropertyConfig config, String sessionFactoryBeanName) {

    NamingStrategy namingStrategy = getNamingStrategy(sessionFactoryBeanName);

    SimpleValue element;//from   w w w  . j  av  a 2 s .c o  m
    if (property.isBasicCollectionType()) {
        element = new SimpleValue(mappings, collection.getCollectionTable());
    } else {
        // for a normal unidirectional one-to-many we use a join column
        element = new ManyToOne(mappings, collection.getCollectionTable());
        bindUnidirectionalOneToManyInverseValues(property, (ManyToOne) element);
    }
    collection.setInverse(false);

    String columnName;

    final boolean hasJoinColumnMapping = hasJoinColumnMapping(config);
    if (property.isBasicCollectionType()) {
        final Class<?> referencedType = property.getReferencedPropertyType();
        String className = referencedType.getName();
        final boolean isEnum = GrailsClassUtils.isJdk5Enum(referencedType);
        if (hasJoinColumnMapping) {
            columnName = config.getJoinTable().getColumn().getName();
        } else {
            columnName = isEnum ? namingStrategy.propertyToColumnName(className)
                    : addUnderscore(namingStrategy.propertyToColumnName(property.getName()),
                            namingStrategy.propertyToColumnName(className));
        }

        if (isEnum) {
            bindEnumType(property, referencedType, element, columnName);
        } else {

            String typeName = getTypeName(property, config, getMapping(property.getDomainClass()));
            if (typeName == null) {
                Type type = mappings.getTypeResolver().basic(className);
                if (type != null) {
                    typeName = type.getName();
                }
            }
            if (typeName == null) {
                String domainName = property.getDomainClass().getName();
                throw new MappingException("Missing type or column for column[" + columnName + "] on domain["
                        + domainName + "] referencing[" + className + "]");
            }

            bindSimpleValue(typeName, element, true, columnName, mappings);
            if (hasJoinColumnMapping) {
                bindColumnConfigToColumn(getColumnForSimpleValue(element), config.getJoinTable().getColumn());
            }
        }
    } else {
        final GrailsDomainClass domainClass = property.getReferencedDomainClass();

        Mapping m = getMapping(domainClass.getClazz());
        if (hasCompositeIdentifier(m)) {
            CompositeIdentity ci = (CompositeIdentity) m.getIdentity();
            bindCompositeIdentifierToManyToOne(property, element, ci, domainClass, EMPTY_PATH,
                    sessionFactoryBeanName);
        } else {
            if (hasJoinColumnMapping) {
                columnName = config.getJoinTable().getColumn().getName();
            } else {
                columnName = namingStrategy.propertyToColumnName(domainClass.getPropertyName())
                        + FOREIGN_KEY_SUFFIX;
            }

            bindSimpleValue("long", element, true, columnName, mappings);
        }
    }

    collection.setElement(element);

    bindCollectionForPropertyConfig(collection, config);
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java

License:Apache License

/**
 * Calculates the mapping table for a many-to-many. One side of
 * the relationship has to "own" the relationship so that there is not a situation
 * where you have two mapping tables for left_right and right_left
 *///from  ww w  .j ava 2s. c om
private static String calculateTableForMany(GrailsDomainClassProperty property, String sessionFactoryBeanName) {
    NamingStrategy namingStrategy = getNamingStrategy(sessionFactoryBeanName);

    String propertyColumnName = namingStrategy.propertyToColumnName(property.getName());
    //fix for GRAILS-5895
    PropertyConfig config = getPropertyConfig(property);
    JoinTable jt = config != null ? config.getJoinTable() : null;
    boolean hasJoinTableMapping = jt != null && jt.getName() != null;
    String left = getTableName(property.getDomainClass(), sessionFactoryBeanName);

    if (Map.class.isAssignableFrom(property.getType())) {
        if (hasJoinTableMapping) {
            return jt.getName();
        }
        return addUnderscore(left, propertyColumnName);
    }

    if (property.isBasicCollectionType()) {
        if (hasJoinTableMapping) {
            return jt.getName();
        }
        return addUnderscore(left, propertyColumnName);
    }

    String right = getTableName(property.getReferencedDomainClass(), sessionFactoryBeanName);

    if (property.isManyToMany()) {
        if (hasJoinTableMapping) {
            return jt.getName();
        }
        if (property.isOwningSide()) {
            return addUnderscore(left, propertyColumnName);
        }
        return addUnderscore(right, namingStrategy.propertyToColumnName(property.getOtherSide().getName()));
    }

    if (shouldCollectionBindWithJoinColumn(property)) {
        if (hasJoinTableMapping) {
            return jt.getName();
        }
        left = trimBackTigs(left);
        right = trimBackTigs(right);
        return addUnderscore(left, right);
    }

    if (property.isOwningSide()) {
        return addUnderscore(left, right);
    }
    return addUnderscore(right, left);
}