List of usage examples for org.hibernate.mapping Formula setFormula
public void setFormula(String string)
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 *//* w w w . jav a 2 s .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 {/*from w w w .ja v a2 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); 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 ww . j a v a 2s . co 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 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); 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 *//* ww w .ja va 2 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 w w w. j a va2 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); } } }
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 ww . ja v a 2 s . com*/ 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 a va 2 s. com 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); } } }
From source file:org.unitime.commons.hibernate.util.HibernateUtil.java
License:Open Source License
public static void fixSchemaInFormulas(Configuration cfg) { cfg.buildMappings();/*from w w w . j av a 2 s. c o m*/ String schema = cfg.getProperty("default_schema"); if (schema != null) { for (Iterator i = cfg.getClassMappings(); i.hasNext();) { PersistentClass pc = (PersistentClass) i.next(); for (Iterator j = pc.getPropertyIterator(); j.hasNext();) { Property p = (Property) j.next(); for (Iterator k = p.getColumnIterator(); k.hasNext();) { Selectable c = (Selectable) k.next(); if (c instanceof Formula) { Formula f = (Formula) c; if (f.getFormula() != null && f.getFormula().indexOf("%SCHEMA%") >= 0) { f.setFormula(f.getFormula().replaceAll("%SCHEMA%", schema)); sLog.debug("Schema updated in " + pc.getClassName() + "." + p.getName() + " to " + f.getFormula()); } } } } } } }