Example usage for org.hibernate.mapping Column getName

List of usage examples for org.hibernate.mapping Column getName

Introduction

In this page you can find the example usage for org.hibernate.mapping Column getName.

Prototype

public String getName() 

Source Link

Usage

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

License:Apache License

protected void linkValueUsingAColumnCopy(GrailsDomainClassProperty prop, Column column, DependantValue key) {
    Column mappingColumn = new Column();
    mappingColumn.setName(column.getName());
    mappingColumn.setLength(column.getLength());
    mappingColumn.setNullable(prop.isOptional());
    mappingColumn.setSqlType(column.getSqlType());

    mappingColumn.setValue(key);// ww  w .ja v a 2 s. c  om
    key.addColumn(mappingColumn);
    key.getTable().addColumn(mappingColumn);
}

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

License:Apache License

protected void bindEnumType(GrailsDomainClassProperty property, Class<?> propertyType, SimpleValue simpleValue,
        String columnName) {/*from  w ww  .j a v  a 2 s.  c om*/

    PropertyConfig pc = getPropertyConfig(property);
    String typeName = getTypeName(property, getPropertyConfig(property), getMapping(property.getDomainClass()));
    if (typeName == null) {
        Properties enumProperties = new Properties();
        enumProperties.put(ENUM_CLASS_PROP, propertyType.getName());

        String enumType = pc == null ? DEFAULT_ENUM_TYPE : pc.getEnumType();
        if (enumType.equals(DEFAULT_ENUM_TYPE) && identityEnumTypeSupports(propertyType)) {
            simpleValue.setTypeName("org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType");
        } else {
            simpleValue.setTypeName(ENUM_TYPE_CLASS);
            if (enumType.equals(DEFAULT_ENUM_TYPE) || "string".equalsIgnoreCase(enumType)) {
                enumProperties.put(ENUM_TYPE_PROP, String.valueOf(Types.VARCHAR));
            } else if (!"ordinal".equalsIgnoreCase(enumType)) {
                LOG.warn("Invalid enumType specified when mapping property [" + property.getName()
                        + "] of class [" + property.getDomainClass().getClazz().getName()
                        + "]. Using defaults instead.");
            }
        }
        simpleValue.setTypeParameters(enumProperties);
    } else {
        simpleValue.setTypeName(typeName);
    }

    Table t = simpleValue.getTable();
    Column column = new Column();

    if (property.getDomainClass().isRoot()) {
        column.setNullable(property.isOptional());
    } else {
        Mapping mapping = getMapping(property.getDomainClass());
        if (mapping == null || mapping.getTablePerHierarchy()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("[GrailsDomainBinder] Sub class property [" + property.getName()
                        + "] for column name [" + column.getName() + "] set to nullable");
            }
            column.setNullable(true);
        } else {
            column.setNullable(property.isOptional());
        }
    }
    column.setValue(simpleValue);
    column.setName(columnName);
    if (t != null)
        t.addColumn(column);

    simpleValue.addColumn(column);

    PropertyConfig propertyConfig = getPropertyConfig(property);
    if (propertyConfig != null && !propertyConfig.getColumns().isEmpty()) {
        bindIndex(columnName, column, propertyConfig.getColumns().get(0), t);
        bindColumnConfigToColumn(column, propertyConfig.getColumns().get(0));
    }
}

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

License:Apache License

/**
 * Binds a Column instance to the Hibernate meta model
 *
 * @param property The Grails domain class property
 * @param parentProperty/*from  ww w.  java 2  s.c  o  m*/
 * @param column     The column to bind
 * @param path
 * @param table      The table name
 * @param sessionFactoryBeanName  the session factory bean name
 */
protected void bindColumn(GrailsDomainClassProperty property, GrailsDomainClassProperty parentProperty,
        Column column, ColumnConfig cc, String path, Table table, String sessionFactoryBeanName) {

    if (cc != null) {
        column.setComment(cc.getComment());
        column.setDefaultValue(cc.getDefaultValue());
        column.setCustomRead(cc.getRead());
        column.setCustomWrite(cc.getWrite());
    }

    Class<?> userType = getUserType(property);
    String columnName = getColumnNameForPropertyAndPath(property, path, cc, sessionFactoryBeanName);
    if ((property.isAssociation() || property.isBasicCollectionType()) && userType == null) {
        // Only use conventional naming when the column has not been explicitly mapped.
        if (column.getName() == null) {
            column.setName(columnName);
        }
        if (property.isManyToMany()) {
            column.setNullable(false);
        } else if (property.isOneToOne() && property.isBidirectional() && !property.isOwningSide()) {
            if (property.getOtherSide().isHasOne()) {
                column.setNullable(false);
            } else {
                column.setNullable(true);
            }
        } else if ((property.isManyToOne() || property.isOneToOne()) && property.isCircular()) {
            column.setNullable(true);
        } else {
            column.setNullable(property.isOptional());
        }
    } else {
        column.setName(columnName);
        column.setNullable(property.isOptional() || (parentProperty != null && parentProperty.isOptional()));

        // Use the constraints for this property to more accurately define
        // the column's length, precision, and scale
        ConstrainedProperty constrainedProperty = getConstrainedProperty(property);
        if (constrainedProperty != null) {
            if (String.class.isAssignableFrom(property.getType())
                    || byte[].class.isAssignableFrom(property.getType())) {
                bindStringColumnConstraints(column, constrainedProperty);
            }

            if (Number.class.isAssignableFrom(property.getType())) {
                bindNumericColumnConstraints(column, constrainedProperty, cc);
            }
        }
    }

    handleUniqueConstraint(property, column, path, table, columnName, sessionFactoryBeanName);

    bindIndex(columnName, column, cc, table);

    if (!property.getDomainClass().isRoot()) {
        Mapping mapping = getMapping(property.getDomainClass());
        if (mapping == null || mapping.getTablePerHierarchy()) {
            if (LOG.isDebugEnabled())
                LOG.debug("[GrailsDomainBinder] Sub class property [" + property.getName()
                        + "] for column name [" + column.getName() + "] set to nullable");
            column.setNullable(true);
        } else {
            column.setNullable(property.isOptional());
        }
    }

    if (LOG.isDebugEnabled())
        LOG.debug("[GrailsDomainBinder] bound property [" + property.getName() + "] to column name ["
                + column.getName() + "] in table [" + table.getName() + "]");
}

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

License:Apache License

private static void linkValueUsingAColumnCopy(GrailsDomainClassProperty prop, Column column,
        DependantValue key) {/*w ww  . j a v a 2  s  .co m*/
    Column mappingColumn = new Column();
    mappingColumn.setName(column.getName());
    mappingColumn.setLength(column.getLength());
    mappingColumn.setNullable(prop.isOptional());
    mappingColumn.setSqlType(column.getSqlType());

    mappingColumn.setValue(key);
    key.addColumn(mappingColumn);
    key.getTable().addColumn(mappingColumn);
}

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

License:Apache License

private static void bindEnumType(GrailsDomainClassProperty property, Class<?> propertyType,
        SimpleValue simpleValue, String columnName) {
    Properties enumProperties = new Properties();
    enumProperties.put(ENUM_CLASS_PROP, propertyType.getName());

    PropertyConfig pc = getPropertyConfig(property);
    String enumType = pc != null ? pc.getEnumType() : DEFAULT_ENUM_TYPE;
    if (enumType.equals(DEFAULT_ENUM_TYPE) && IdentityEnumType.supports(propertyType)) {
        simpleValue.setTypeName(IdentityEnumType.class.getName());
    } else {/*from  w  ww.  j a v  a  2 s  .  c  om*/
        simpleValue.setTypeName(ENUM_TYPE_CLASS);
        if (enumType.equals(DEFAULT_ENUM_TYPE) || "string".equalsIgnoreCase(enumType)) {
            enumProperties.put(ENUM_TYPE_PROP, String.valueOf(Types.VARCHAR));
        } else if (!"ordinal".equalsIgnoreCase(enumType)) {
            LOG.warn("Invalid enumType specified when mapping property [" + property.getName() + "] of class ["
                    + property.getDomainClass().getClazz().getName() + "]. Using defaults instead.");
        }
    }

    simpleValue.setTypeParameters(enumProperties);
    Table t = simpleValue.getTable();
    Column column = new Column();

    if (property.getDomainClass().isRoot()) {
        column.setNullable(property.isOptional());
    } else {
        Mapping mapping = getMapping(property.getDomainClass());
        if (mapping == null || mapping.getTablePerHierarchy()) {
            if (LOG.isDebugEnabled())
                LOG.debug("[GrailsDomainBinder] Sub class property [" + property.getName()
                        + "] for column name [" + column.getName() + "] set to nullable");
            column.setNullable(true);
        } else {
            column.setNullable(property.isOptional());
        }
    }
    column.setValue(simpleValue);
    column.setName(columnName);
    if (t != null)
        t.addColumn(column);

    simpleValue.addColumn(column);

    PropertyConfig propertyConfig = getPropertyConfig(property);
    if (propertyConfig != null && !propertyConfig.getColumns().isEmpty()) {
        bindIndex(columnName, column, propertyConfig.getColumns().get(0), t);
    }
}

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

License:Apache License

/**
 * Binds a Column instance to the Hibernate meta model
 *
 * @param property The Grails domain class property
 * @param parentProperty/*from w ww.j  a  v  a 2s.c om*/
 * @param column     The column to bind
 * @param path
 * @param table      The table name
 * @param sessionFactoryBeanName  the session factory bean name
 */
private static void bindColumn(GrailsDomainClassProperty property, GrailsDomainClassProperty parentProperty,
        Column column, ColumnConfig cc, String path, Table table, String sessionFactoryBeanName) {

    Class<?> userType = getUserType(property);
    String columnName = getColumnNameForPropertyAndPath(property, path, cc, sessionFactoryBeanName);
    if ((property.isAssociation() || property.isBasicCollectionType()) && userType == null) {
        // Only use conventional naming when the column has not been explicitly mapped.
        if (column.getName() == null) {
            column.setName(columnName);
        }
        if (property.isManyToMany()) {
            column.setNullable(false);
        } else if (property.isOneToOne() && property.isBidirectional() && !property.isOwningSide()) {
            if (property.getOtherSide().isHasOne()) {
                column.setNullable(false);
            } else {
                column.setNullable(true);
            }
        } else if ((property.isManyToOne() || property.isOneToOne()) && property.isCircular()) {
            column.setNullable(true);
        } else {
            column.setNullable(property.isOptional());
        }
    } else {
        column.setName(columnName);
        column.setNullable(property.isOptional() || (parentProperty != null && parentProperty.isOptional()));

        // Use the constraints for this property to more accurately define
        // the column's length, precision, and scale
        ConstrainedProperty constrainedProperty = getConstrainedProperty(property);
        if (constrainedProperty != null) {
            if (String.class.isAssignableFrom(property.getType())
                    || byte[].class.isAssignableFrom(property.getType())) {
                bindStringColumnConstraints(column, constrainedProperty);
            }

            if (Number.class.isAssignableFrom(property.getType())) {
                bindNumericColumnConstraints(column, constrainedProperty);
            }
        }
    }

    ConstrainedProperty cp = getConstrainedProperty(property);
    if (cp != null && cp.hasAppliedConstraint(UniqueConstraint.UNIQUE_CONSTRAINT)) {
        UniqueConstraint uc = (UniqueConstraint) cp.getAppliedConstraint(UniqueConstraint.UNIQUE_CONSTRAINT);
        if (uc != null && uc.isUnique()) {
            if (!uc.isUniqueWithinGroup()) {
                column.setUnique(true);
            } else if (uc.getUniquenessGroup().size() > 0) {
                createKeyForProps(property, path, table, columnName, uc.getUniquenessGroup(),
                        sessionFactoryBeanName);
            }
        }
    } else {
        Object val = cp != null ? cp.getMetaConstraintValue(UniqueConstraint.UNIQUE_CONSTRAINT) : null;
        if (val instanceof Boolean) {
            column.setUnique(((Boolean) val).booleanValue());
        } else if (val instanceof String) {
            createKeyForProps(property, path, table, columnName, Arrays.asList(new String[] { (String) val }),
                    sessionFactoryBeanName);
        } else if (val instanceof List<?> && ((List<?>) val).size() > 0) {
            createKeyForProps(property, path, table, columnName, (List<?>) val, sessionFactoryBeanName);
        }
    }

    bindIndex(columnName, column, cc, table);

    if (!property.getDomainClass().isRoot()) {
        Mapping mapping = getMapping(property.getDomainClass());
        if (mapping == null || mapping.getTablePerHierarchy()) {
            if (LOG.isDebugEnabled())
                LOG.debug("[GrailsDomainBinder] Sub class property [" + property.getName()
                        + "] for column name [" + column.getName() + "] set to nullable");
            column.setNullable(true);
        } else {
            column.setNullable(property.isOptional());
        }
    }

    if (LOG.isDebugEnabled())
        LOG.debug("[GrailsDomainBinder] bound property [" + property.getName() + "] to column name ["
                + column.getName() + "] in table [" + table.getName() + "]");
}

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

License:Apache License

/**
 * Tests that single- and multi-column user type mappings work
 * correctly. Also Checks that the "sqlType" property is honoured.
 *///from  w ww.ja v a  2 s . co  m
public void testUserTypeMappings() {
    DefaultGrailsDomainConfiguration config = getDomainConfig(MULTI_COLUMN_USER_TYPE_DEFINITION);
    PersistentClass persistentClass = config.getClassMapping("Item");

    // First check the "name" property and its associated column.
    Property nameProperty = persistentClass.getProperty("name");
    assertEquals(1, nameProperty.getColumnSpan());
    assertEquals("name", nameProperty.getName());

    Column column = (Column) nameProperty.getColumnIterator().next();
    assertEquals("s_name", column.getName());
    assertEquals("text", column.getSqlType());

    // Next the "other" property.
    Property otherProperty = persistentClass.getProperty("other");
    assertEquals(1, otherProperty.getColumnSpan());
    assertEquals("other", otherProperty.getName());

    column = (Column) otherProperty.getColumnIterator().next();
    assertEquals("other", column.getName());
    assertEquals("wrapper-characters", column.getSqlType());
    assertEquals(MyUserType.class.getName(), column.getValue().getType().getName());
    assertTrue(column.getValue() instanceof SimpleValue);
    SimpleValue v = (SimpleValue) column.getValue();
    assertEquals("myParam1", v.getTypeParameters().get("param1"));
    assertEquals("myParam2", v.getTypeParameters().get("param2"));

    // And now for the "price" property, which should have two
    // columns.
    Property priceProperty = persistentClass.getProperty("price");
    assertEquals(2, priceProperty.getColumnSpan());
    assertEquals("price", priceProperty.getName());

    Iterator colIter = priceProperty.getColumnIterator();
    column = (Column) colIter.next();
    assertEquals("value", column.getName());
    assertNull("SQL type should have been 'null' for 'value' column.", column.getSqlType());

    column = (Column) colIter.next();
    assertEquals("currency_code", column.getName());
    assertEquals("text", column.getSqlType());
}

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

License:Apache License

public void testForeignKeyColumnBinding() {
    GroovyClassLoader cl = new GroovyClassLoader();
    GrailsDomainClass oneClass = new DefaultGrailsDomainClass(
            cl.parseClass("class TestOneSide {\n" + "    Long id \n" + "    Long version \n"
                    + "    String name \n" + "    String description \n" + "}"));
    GrailsDomainClass domainClass = new DefaultGrailsDomainClass(cl.parseClass("class TestManySide {\n"
            + "    Long id \n" + "    Long version \n" + "    String name \n" + "    TestOneSide testOneSide \n"
            + "\n" + "    static mapping = {\n" + "        columns {\n"
            + "            testOneSide column:'EXPECTED_COLUMN_NAME'" + "        }\n" + "    }\n" + "}"));

    DefaultGrailsDomainConfiguration config = getDomainConfig(cl,
            new Class[] { oneClass.getClazz(), domainClass.getClazz() });

    PersistentClass persistentClass = config.getClassMapping("TestManySide");

    Column column = (Column) persistentClass.getProperty("testOneSide").getColumnIterator().next();
    assertEquals("EXPECTED_COLUMN_NAME", column.getName());
}

From source file:org.eclipse.emf.teneo.hibernate.HbDataStore.java

License:Open Source License

/** Checks if a certain column already exists in a class */
private Column checkColumnExists(Table table, Column searchCol) {
    for (int i = 0; i < table.getColumnSpan(); i++) {
        final Column column = table.getColumn(i);
        if (stripQuotes(column.getName()).equalsIgnoreCase(searchCol.getName())) {
            return column;
        }//from  ww w  .java  2  s .com
    }
    table.addColumn(searchCol);
    return searchCol;
}

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

License:Apache License

protected void linkValueUsingAColumnCopy(PersistentProperty prop, Column column, DependantValue key) {
    Column mappingColumn = new Column();
    mappingColumn.setName(column.getName());
    mappingColumn.setLength(column.getLength());
    mappingColumn.setNullable(prop.isNullable());
    mappingColumn.setSqlType(column.getSqlType());

    mappingColumn.setValue(key);/*ww w.jav  a 2  s  . c om*/
    key.addColumn(mappingColumn);
    key.getTable().addColumn(mappingColumn);
}