Example usage for org.hibernate.mapping Column setDefaultValue

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

Introduction

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

Prototype

public void setDefaultValue(String defaultValue) 

Source Link

Usage

From source file:net.lshift.hibernate.migrations.AlterTableBuilder.java

License:Apache License

private static Column buildColumnDefinition(String name, int sqlType, int length, boolean nullable,
        Object defaultVal) {/*w  w  w. ja va 2s .  c om*/
    Column col = new Column(name);
    col.setSqlTypeCode(sqlType);
    col.setNullable(nullable);
    col.setLength(length);
    col.setDefaultValue(defaultVal != null ? defaultVal.toString() : null);
    return col;
}

From source file:net.lshift.hibernate.migrations.CreateTableBuilder.java

License:Apache License

public CreateTableBuilder column(String name, int sqlType, int length, boolean nullable, Object defaultVal) {
    Column col = new Column(name);
    col.setNullable(nullable);//from ww  w .  j  ava  2 s  .  c o m
    col.setSqlTypeCode(sqlType);
    col.setLength(length);
    col.setDefaultValue(defaultVal != null ? defaultVal.toString() : null);

    columns.add(col);

    return this;
}

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  w  w w.  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
 */
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.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  w ww .j  ava2s .  com
 * @param column     The column to bind
 * @param path
 * @param table      The table name
 * @param sessionFactoryBeanName  the session factory bean name
 */
protected void bindColumn(PersistentProperty property, PersistentProperty 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 instanceof Association) && userType == null) {
        Association association = (Association) property;
        // Only use conventional naming when the column has not been explicitly mapped.
        if (column.getName() == null) {
            column.setName(columnName);
        }
        if (property instanceof ManyToMany) {
            column.setNullable(false);
        } else if (property instanceof org.grails.datastore.mapping.model.types.OneToOne
                && association.isBidirectional() && !association.isOwningSide()) {
            if (isHasOne(((Association) property).getInverseSide())) {
                column.setNullable(false);
            } else {
                column.setNullable(true);
            }
        } else if ((property instanceof ToOne) && association.isCircular()) {
            column.setNullable(true);
        } else {
            column.setNullable(property.isNullable());
        }
    } else {
        column.setName(columnName);
        column.setNullable(property.isNullable() || (parentProperty != null && parentProperty.isNullable()));

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

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

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

    bindIndex(columnName, column, cc, table);

    final PersistentEntity owner = property.getOwner();
    if (!owner.isRoot()) {
        Mapping mapping = getMapping(owner);
        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.isNullable());
        }
    }

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