Search in sources :

Example 1 with Formula

use of org.hibernate.mapping.Formula in project hibernate-orm by hibernate.

the class RelationalObjectBinder method bindColumnsAndFormulas.

public void bindColumnsAndFormulas(MappingDocument sourceDocument, List<RelationalValueSource> relationalValueSources, SimpleValue simpleValue, boolean areColumnsNullableByDefault, ColumnNamingDelegate columnNamingDelegate) {
    for (RelationalValueSource relationalValueSource : relationalValueSources) {
        if (ColumnSource.class.isInstance(relationalValueSource)) {
            final ColumnSource columnSource = (ColumnSource) relationalValueSource;
            bindColumn(sourceDocument, columnSource, simpleValue, areColumnsNullableByDefault, columnNamingDelegate);
        } else {
            final DerivedValueSource formulaSource = (DerivedValueSource) relationalValueSource;
            simpleValue.addFormula(new Formula(formulaSource.getExpression()));
        }
    }
}
Also used : Formula(org.hibernate.mapping.Formula) RelationalValueSource(org.hibernate.boot.model.source.spi.RelationalValueSource) DerivedValueSource(org.hibernate.boot.model.source.spi.DerivedValueSource) ColumnSource(org.hibernate.boot.model.source.spi.ColumnSource)

Example 2 with Formula

use of org.hibernate.mapping.Formula in project hibernate-orm by hibernate.

the class Ejb3Column method bind.

public void bind() {
    if (StringHelper.isNotEmpty(formulaString)) {
        LOG.debugf("Binding formula %s", formulaString);
        formula = new Formula();
        formula.setFormula(formulaString);
    } else {
        initMappingColumn(logicalColumnName, propertyName, length, precision, scale, nullable, sqlType, unique, true);
        if (defaultValue != null) {
            mappingColumn.setDefaultValue(defaultValue);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debugf("Binding column: %s", toString());
        }
    }
}
Also used : Formula(org.hibernate.mapping.Formula)

Example 3 with Formula

use of org.hibernate.mapping.Formula in project hibernate-orm by hibernate.

the class CompositeElementTest method afterMetadataBuilt.

@Override
protected void afterMetadataBuilt(Metadata metadata) {
    Collection children = metadata.getCollectionBinding(Parent.class.getName() + ".children");
    Component childComponents = (Component) children.getElement();
    Formula f = (Formula) childComponents.getProperty("bioLength").getValue().getColumnIterator().next();
    SQLFunction lengthFunction = metadata.getDatabase().getJdbcEnvironment().getDialect().getFunctions().get("length");
    if (lengthFunction != null) {
        ArrayList args = new ArrayList();
        args.add("bio");
        f.setFormula(lengthFunction.render(StandardBasicTypes.INTEGER, args, null));
    }
}
Also used : Formula(org.hibernate.mapping.Formula) ArrayList(java.util.ArrayList) Collection(org.hibernate.mapping.Collection) SQLFunction(org.hibernate.dialect.function.SQLFunction) Component(org.hibernate.mapping.Component)

Example 4 with Formula

use of org.hibernate.mapping.Formula in project hibernate-orm by hibernate.

the class ComponentTest method afterMetadataBuilt.

@Override
protected void afterMetadataBuilt(Metadata metadata) {
    // Oracle and Postgres do not have year() functions, so we need to
    // redefine the 'User.person.yob' formula
    // 
    // consider temporary until we add the capability to define
    // mapping formulas which can use dialect-registered functions...
    PersistentClass user = metadata.getEntityBinding(User.class.getName());
    org.hibernate.mapping.Property personProperty = user.getProperty("person");
    Component component = (Component) personProperty.getValue();
    Formula f = (Formula) component.getProperty("yob").getValue().getColumnIterator().next();
    SQLFunction yearFunction = metadata.getDatabase().getJdbcEnvironment().getDialect().getFunctions().get("year");
    if (yearFunction == null) {
        // the dialect not know to support a year() function, so rely on the
        // ANSI SQL extract function
        f.setFormula("extract( year from dob )");
    } else {
        List args = new ArrayList();
        args.add("dob");
        f.setFormula(yearFunction.render(StandardBasicTypes.INTEGER, args, null));
    }
}
Also used : Formula(org.hibernate.mapping.Formula) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SQLFunction(org.hibernate.dialect.function.SQLFunction) Component(org.hibernate.mapping.Component) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 5 with Formula

use of org.hibernate.mapping.Formula in project hibernate-orm by hibernate.

the class Ejb3Column method initMappingColumn.

protected void initMappingColumn(String columnName, String propertyName, int length, int precision, int scale, boolean nullable, String sqlType, boolean unique, boolean applyNamingStrategy) {
    if (StringHelper.isNotEmpty(formulaString)) {
        this.formula = new Formula();
        this.formula.setFormula(formulaString);
    } else {
        this.mappingColumn = new Column();
        redefineColumnName(columnName, propertyName, applyNamingStrategy);
        this.mappingColumn.setLength(length);
        if (precision > 0) {
            // revelent precision
            this.mappingColumn.setPrecision(precision);
            this.mappingColumn.setScale(scale);
        }
        this.mappingColumn.setNullable(nullable);
        this.mappingColumn.setSqlType(sqlType);
        this.mappingColumn.setUnique(unique);
        if (writeExpression != null && !writeExpression.matches("[^?]*\\?[^?]*")) {
            throw new AnnotationException("@WriteExpression must contain exactly one value placeholder ('?') character: property [" + propertyName + "] and column [" + logicalColumnName + "]");
        }
        if (readExpression != null) {
            this.mappingColumn.setCustomRead(readExpression);
        }
        if (writeExpression != null) {
            this.mappingColumn.setCustomWrite(writeExpression);
        }
    }
}
Also used : Formula(org.hibernate.mapping.Formula) Column(org.hibernate.mapping.Column) AnnotationException(org.hibernate.AnnotationException)

Aggregations

Formula (org.hibernate.mapping.Formula)7 Component (org.hibernate.mapping.Component)3 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 AnnotationException (org.hibernate.AnnotationException)2 SQLFunction (org.hibernate.dialect.function.SQLFunction)2 Column (org.hibernate.mapping.Column)2 List (java.util.List)1 Random (java.util.Random)1 MapKeyColumn (javax.persistence.MapKeyColumn)1 MapKeyJoinColumn (javax.persistence.MapKeyJoinColumn)1 AssertionFailure (org.hibernate.AssertionFailure)1 XProperty (org.hibernate.annotations.common.reflection.XProperty)1 ColumnSource (org.hibernate.boot.model.source.spi.ColumnSource)1 DerivedValueSource (org.hibernate.boot.model.source.spi.DerivedValueSource)1 RelationalValueSource (org.hibernate.boot.model.source.spi.RelationalValueSource)1 Ejb3Column (org.hibernate.cfg.Ejb3Column)1 Ejb3JoinColumn (org.hibernate.cfg.Ejb3JoinColumn)1 HSQLDialect (org.hibernate.dialect.HSQLDialect)1 Collection (org.hibernate.mapping.Collection)1