Example usage for org.hibernate.mapping Formula Formula

List of usage examples for org.hibernate.mapping Formula Formula

Introduction

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

Prototype

public Formula() 

Source Link

Usage

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

License:Apache License

/**
 * Creates and binds the discriminator property used in table-per-hierarchy inheritance to
 * discriminate between sub class instances
 *
 * @param table    The table to bind onto
 * @param entity   The root class entity
 * @param mappings The mappings instance
 *///from w  w w  .java  2s.  co m
protected void bindDiscriminatorProperty(Table table, RootClass entity, Mappings mappings) {
    Mapping m = getMapping(entity.getMappedClass());
    SimpleValue d = new SimpleValue(mappings, table);
    entity.setDiscriminator(d);
    entity.setDiscriminatorValue(
            m != null && m.getDiscriminator() != null ? m.getDiscriminator() : entity.getClassName());

    if (m != null && m.getDiscriminatorMap().get("insert") != null) {
        entity.setDiscriminatorInsertable((Boolean) m.getDiscriminatorMap().get("insert"));
    }
    if (m != null && m.getDiscriminatorMap().get("type") != null) {
        d.setTypeName((String) m.getDiscriminatorMap().get("type"));
    }

    if (m != null && m.getDiscriminatorMap().get("formula") != null) {
        Formula formula = new Formula();
        formula.setFormula((String) m.getDiscriminatorMap().get("formula"));
        d.addFormula(formula);
    } else {
        bindSimpleValue(STRING_TYPE, d, false, RootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME, mappings);

        ColumnConfig cc = m == null ? null : m.getDiscriminatorColumn();
        if (cc != null) {
            Column c = (Column) d.getColumnIterator().next();
            if (cc.getName() != null) {
                c.setName(cc.getName());
            }
            bindColumnConfigToColumn(c, cc);
        }
    }

    entity.setPolymorphic(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 ww .ja  va2s  .  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);

            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

/**
 * Creates and binds the discriminator property used in table-per-hierarchy inheritance to
 * discriminate between sub class instances
 *
 * @param table    The table to bind onto
 * @param entity   The root class entity
 * @param mappings The mappings instance
 *///from w  w w. j ava  2s  .  c o m
private static void bindDiscriminatorProperty(Table table, RootClass entity, Mappings mappings) {
    Mapping m = getMapping(entity.getMappedClass());
    SimpleValue d = new SimpleValue(mappings, table);
    entity.setDiscriminator(d);
    entity.setDiscriminatorValue(
            m != null && m.getDiscriminator() != null ? m.getDiscriminator() : entity.getClassName());

    if (m != null && m.getDiscriminatorMap().get("insert") != null) {
        entity.setDiscriminatorInsertable((Boolean) m.getDiscriminatorMap().get("insert"));
    }
    if (m != null && m.getDiscriminatorMap().get("type") != null) {
        d.setTypeName((String) m.getDiscriminatorMap().get("type"));
    }

    if (m != null && m.getDiscriminatorMap().get("formula") != null) {
        Formula formula = new Formula();
        formula.setFormula((String) m.getDiscriminatorMap().get("formula"));
        d.addFormula(formula);
    } else {
        bindSimpleValue(STRING_TYPE, d, false, RootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME, mappings);

        ColumnConfig cc = m == null ? null : m.getDiscriminatorColumn();
        if (cc != null) {
            Column c = (Column) d.getColumnIterator().next();
            if (cc.getName() != null) {
                c.setName(cc.getName());
            }
            bindColumnConfigToColumn(c, cc);
        }
    }

    entity.setPolymorphic(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  a  va  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 (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.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

/**
 * Creates and binds the discriminator property used in table-per-hierarchy inheritance to
 * discriminate between sub class instances
 *
 * @param table    The table to bind onto
 * @param entity   The root class entity
 * @param mappings The mappings instance
 *//*from   w ww.  j  a va2 s  .c om*/
protected void bindDiscriminatorProperty(Table table, RootClass entity, Mappings mappings) {
    Mapping m = getMapping(entity.getMappedClass());
    SimpleValue d = new SimpleValue(mappings, table);
    entity.setDiscriminator(d);
    entity.setDiscriminatorValue(
            m != null && m.getDiscriminator() != null ? m.getDiscriminator() : entity.getClassName());

    if (m != null && m.getDiscriminatorMap().get("insert") != null) {
        entity.setDiscriminatorInsertable((Boolean) m.getDiscriminatorMap().get("insert"));
    }
    if (m != null && m.getDiscriminatorMap().get("type") != null) {
        d.setTypeName((String) m.getDiscriminatorMap().get("type"));
    }

    if (m != null && m.getDiscriminatorMap().get("formula") != null) {
        Formula formula = new Formula();
        formula.setFormula((String) m.getDiscriminatorMap().get("formula"));
        d.addFormula(formula);
    } else {
        bindSimpleValue(STRING_TYPE, d, false, RootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME, mappings);

        ColumnConfig cc = m == null ? null : m.getDiscriminatorColumn();
        if (cc != null) {
            Column c = (Column) d.getColumnIterator().next();
            if (cc.getName() != null) {
                c.setName(cc.getName());
            }
            bindColumnConfigToColumn(null, c, cc);
        }
    }

    entity.setPolymorphic(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 {/*from ww  w . j  a v a2s  . 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 (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);
        }
    }
}

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

License:Apache License

/**
 * Creates and binds the discriminator property used in table-per-hierarchy inheritance to
 * discriminate between sub class instances
 *
 * @param table    The table to bind onto
 * @param entity   The root class entity
 * @param mappings The mappings instance
 *///from w w  w  .ja v a  2  s. co  m
protected void bindDiscriminatorProperty(Table table, RootClass entity, InFlightMetadataCollector mappings) {
    Mapping m = getMapping(entity.getMappedClass());
    SimpleValue d = new SimpleValue(mappings, table);
    entity.setDiscriminator(d);
    DiscriminatorConfig discriminatorConfig = m != null ? m.getDiscriminator() : null;

    boolean hasDiscriminatorConfig = discriminatorConfig != null;
    entity.setDiscriminatorValue(
            hasDiscriminatorConfig ? discriminatorConfig.getValue() : entity.getClassName());

    if (hasDiscriminatorConfig) {
        if (discriminatorConfig.getInsertable() != null) {
            entity.setDiscriminatorInsertable(discriminatorConfig.getInsertable());
        }
        Object type = discriminatorConfig.getType();
        if (type != null) {
            if (type instanceof Class) {
                d.setTypeName(((Class) type).getName());
            } else {
                d.setTypeName(type.toString());
            }
        }
    }

    if (hasDiscriminatorConfig && discriminatorConfig.getFormula() != null) {
        Formula formula = new Formula();
        formula.setFormula(discriminatorConfig.getFormula());
        d.addFormula(formula);
    } else {
        bindSimpleValue(STRING_TYPE, d, false, RootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME, mappings);

        ColumnConfig cc = !hasDiscriminatorConfig ? null : discriminatorConfig.getColumn();
        if (cc != null) {
            Column c = (Column) d.getColumnIterator().next();
            if (cc.getName() != null) {
                c.setName(cc.getName());
            }
            bindColumnConfigToColumn(null, c, cc);
        }
    }

    entity.setPolymorphic(true);
}

From source file:org.grails.orm.hibernate.cfg.GrailsDomainBinder.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() && !(grailsProp instanceof TenantId)) {
        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();
        boolean hasConfig = propertyConfig != null;

        String generator = hasConfig ? propertyConfig.getGenerator() : null;
        if (generator != null) {
            simpleValue.setIdentifierGeneratorStrategy(generator);
            Properties params = propertyConfig.getTypeParams();
            if (params != null) {
                Properties generatorProps = new Properties();
                generatorProps.putAll(params);

                if (generatorProps.containsKey(SEQUENCE_KEY)) {
                    generatorProps.put(SequenceStyleGenerator.SEQUENCE_PARAM,
                            generatorProps.getProperty(SEQUENCE_KEY));
                }
                simpleValue.setIdentifierGeneratorProperties(generatorProps);
            }
        }

        // 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 = hasConfig ? 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);
        }
    }
}