Example usage for org.hibernate.mapping Column setUnique

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

Introduction

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

Prototype

public void setUnique(boolean unique) 

Source Link

Usage

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

License:Apache License

protected void bindColumnConfigToColumn(Column column, ColumnConfig columnConfig) {
    if (columnConfig == null) {
        return;//from w w w  . j a va2s. c o  m
    }

    if (columnConfig.getLength() != -1) {
        column.setLength(columnConfig.getLength());
    }
    if (columnConfig.getPrecision() != -1) {
        column.setPrecision(columnConfig.getPrecision());
    }
    if (columnConfig.getScale() != -1) {
        column.setScale(columnConfig.getScale());
    }
    if (columnConfig.getSqlType() != null && !columnConfig.getSqlType().isEmpty()) {
        column.setSqlType(columnConfig.getSqlType());
    }
    column.setUnique(columnConfig.getUnique());
}

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 . j  a  v a  2s .c o m*/
@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 void bindSimpleValue(GrailsDomainClassProperty grailsProp, GrailsDomainClassProperty parentProperty,
        SimpleValue simpleValue, String path, PropertyConfig propertyConfig, String sessionFactoryBeanName) {
    setTypeForPropertyConfig(grailsProp, simpleValue, propertyConfig);
    if (grailsProp.isDerived()) {
        Formula formula = new Formula();
        formula.setFormula(propertyConfig.getFormula());
        simpleValue.addFormula(formula);
    } else {/*w  w  w.j av  a 2 s . com*/
        Table table = simpleValue.getTable();

        // Add the column definitions for this value/property. Note that
        // not all custom mapped properties will have column definitions,
        // in which case we still need to create a Hibernate column for
        // this value.
        List<?> columnDefinitions = propertyConfig != null ? propertyConfig.getColumns()
                : Arrays.asList(new Object[] { null });
        if (columnDefinitions.isEmpty()) {
            columnDefinitions = Arrays.asList(new Object[] { null });
        }

        for (int i = 0, n = columnDefinitions.size(); i < n; i++) {
            ColumnConfig cc = (ColumnConfig) columnDefinitions.get(i);
            Column column = new Column();

            // Check for explicitly mapped column name and SQL type.
            if (cc != null) {
                if (cc.getName() != null) {
                    column.setName(cc.getName());
                }
                if (cc.getSqlType() != null) {
                    column.setSqlType(cc.getSqlType());
                }
            }

            column.setValue(simpleValue);

            if (cc != null) {
                if (cc.getLength() != -1) {
                    column.setLength(cc.getLength());
                }
                if (cc.getPrecision() != -1) {
                    column.setPrecision(cc.getPrecision());
                }
                if (cc.getScale() != -1) {
                    column.setScale(cc.getScale());
                }
                column.setUnique(cc.isUnique());
            }

            bindColumn(grailsProp, parentProperty, column, cc, path, table, sessionFactoryBeanName);

            if (table != null) {
                table.addColumn(column);
            }

            simpleValue.addColumn(column);
        }
    }
}

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

License:Apache License

private static void bindColumnConfigToColumn(Column column, ColumnConfig columnConfig) {
    if (columnConfig != null) {
        column.setLength(columnConfig.getLength());
        column.setPrecision(columnConfig.getPrecision());
        column.setSqlType(columnConfig.getSqlType());
        column.setUnique(columnConfig.getUnique());
        column.setScale(columnConfig.getScale());
    }// ww  w . ja  v a 2 s  .c  om
}

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

License:Apache License

/**
 * Binds a many-to-one relationship to the
 *
 *//* ww w  .  ja  va 2s.c  o  m*/
@SuppressWarnings("unchecked")
private static 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.GrailsDomainBinder.java

License:Apache License

private static void bindSimpleValue(GrailsDomainClassProperty grailsProp,
        GrailsDomainClassProperty parentProperty, SimpleValue simpleValue, String path,
        PropertyConfig propertyConfig, String sessionFactoryBeanName) {
    setTypeForPropertyConfig(grailsProp, simpleValue, propertyConfig);
    if (grailsProp.isDerived()) {
        Formula formula = new Formula();
        formula.setFormula(propertyConfig.getFormula());
        simpleValue.addFormula(formula);
    } else {//from w w  w  .j  av  a 2s.  c o  m
        Table table = simpleValue.getTable();

        // Add the column definitions for this value/property. Note that
        // not all custom mapped properties will have column definitions,
        // in which case we still need to create a Hibernate column for
        // this value.
        List<?> columnDefinitions = propertyConfig != null ? propertyConfig.getColumns()
                : Arrays.asList(new Object[] { null });
        if (columnDefinitions.isEmpty()) {
            columnDefinitions = Arrays.asList(new Object[] { null });
        }

        for (int i = 0, n = columnDefinitions.size(); i < n; i++) {
            ColumnConfig cc = (ColumnConfig) columnDefinitions.get(i);
            Column column = new Column();

            // Check for explicitly mapped column name and SQL type.
            if (cc != null) {
                if (cc.getName() != null) {
                    column.setName(cc.getName());
                }
                if (cc.getSqlType() != null) {
                    column.setSqlType(cc.getSqlType());
                }
            }

            column.setValue(simpleValue);
            bindColumn(grailsProp, parentProperty, column, cc, path, table, sessionFactoryBeanName);

            if (cc != null) {
                if (cc.getLength() != -1) {
                    column.setLength(cc.getLength());
                }
                if (cc.getPrecision() != -1) {
                    column.setPrecision(cc.getPrecision());
                }
                if (cc.getScale() != -1) {
                    column.setScale(cc.getScale());
                }
                column.setUnique(cc.isUnique());
            }

            if (table != null)
                table.addColumn(column);

            simpleValue.addColumn(column);
        }
    }
}

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//  w  w  w  . j a va 2  s.com
 * @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.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

protected void bindColumnConfigToColumn(PersistentProperty property, Column column, ColumnConfig columnConfig) {
    final PropertyConfig mappedForm = property != null ? (PropertyConfig) property.getMapping().getMappedForm()
            : null;//from   w  w w.  jav a2  s . c  o  m
    boolean allowUnique = mappedForm != null && !mappedForm.isUniqueWithinGroup();

    if (columnConfig == null) {
        return;
    }

    if (columnConfig.getLength() != -1) {
        column.setLength(columnConfig.getLength());
    }
    if (columnConfig.getPrecision() != -1) {
        column.setPrecision(columnConfig.getPrecision());
    }
    if (columnConfig.getScale() != -1) {
        column.setScale(columnConfig.getScale());
    }
    if (columnConfig.getSqlType() != null && !columnConfig.getSqlType().isEmpty()) {
        column.setSqlType(columnConfig.getSqlType());
    }

    if (allowUnique) {
        column.setUnique(columnConfig.getUnique());
    }
}

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

License:Apache License

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

    NamingStrategy namingStrategy = getNamingStrategy(sessionFactoryBeanName);

    bindManyToOneValues(property, manyToOne);
    PersistentEntity refDomainClass = property instanceof ManyToMany ? property.getOwner()
            : property.getAssociatedEntity();
    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 instanceof ManyToMany)) {
            PropertyConfig pc = getPropertyConfig(property);

            if (pc.getColumns().isEmpty()) {
                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 instanceof org.grails.datastore.mapping.model.types.OneToOne) && !isComposite) {
        manyToOne.setAlternateUniqueKey(true);
        Column c = getColumnForSimpleValue(manyToOne);
        if (config != null && !config.isUniqueWithinGroup()) {
            c.setUnique(config.isUnique());
        } else if (property.isBidirectional() && isHasOne(property.getInverseSide())) {
            c.setUnique(true);
        }
    }
}

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

License:Apache License

protected void bindSimpleValue(PersistentProperty grailsProp, PersistentProperty parentProperty,
        SimpleValue simpleValue, String path, PropertyConfig propertyConfig, String sessionFactoryBeanName) {
    setTypeForPropertyConfig(grailsProp, simpleValue, propertyConfig);
    final PropertyConfig mappedForm = (PropertyConfig) grailsProp.getMapping().getMappedForm();
    if (mappedForm.isDerived()) {
        Formula formula = new Formula();
        formula.setFormula(propertyConfig.getFormula());
        simpleValue.addFormula(formula);
    } else {/*  w  ww  .  j a v a 2 s . c o m*/
        Table table = simpleValue.getTable();

        // Add the column definitions for this value/property. Note that
        // not all custom mapped properties will have column definitions,
        // in which case we still need to create a Hibernate column for
        // this value.
        List<?> columnDefinitions = propertyConfig != null ? propertyConfig.getColumns()
                : Arrays.asList(new Object[] { null });
        if (columnDefinitions.isEmpty()) {
            columnDefinitions = Arrays.asList(new Object[] { null });
        }

        for (Object columnDefinition : columnDefinitions) {
            ColumnConfig cc = (ColumnConfig) columnDefinition;
            Column column = new Column();

            // Check for explicitly mapped column name and SQL type.
            if (cc != null) {
                if (cc.getName() != null) {
                    column.setName(cc.getName());
                }
                if (cc.getSqlType() != null) {
                    column.setSqlType(cc.getSqlType());
                }
            }

            column.setValue(simpleValue);

            if (cc != null) {
                if (cc.getLength() != -1) {
                    column.setLength(cc.getLength());
                }
                if (cc.getPrecision() != -1) {
                    column.setPrecision(cc.getPrecision());
                }
                if (cc.getScale() != -1) {
                    column.setScale(cc.getScale());
                }
                if (!mappedForm.isUniqueWithinGroup()) {
                    column.setUnique(cc.isUnique());
                }
            }

            bindColumn(grailsProp, parentProperty, column, cc, path, table, sessionFactoryBeanName);

            if (table != null) {
                table.addColumn(column);
            }

            simpleValue.addColumn(column);
        }
    }
}