Search in sources :

Example 6 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 7 with Formula

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

the class DiscriminatorFormulaTest method testDiscriminatorFormulaInAuditTable.

@Test
public void testDiscriminatorFormulaInAuditTable() {
    assert parentAudit.getDiscriminator().hasFormula();
    Iterator iterator = parentAudit.getDiscriminator().getColumnIterator();
    while (iterator.hasNext()) {
        Object o = iterator.next();
        if (o instanceof Formula) {
            Formula formula = (Formula) o;
            Assert.assertEquals(ParentEntity.DISCRIMINATOR_QUERY, formula.getText());
            return;
        }
    }
    assert false;
}
Also used : Formula(org.hibernate.mapping.Formula) Iterator(java.util.Iterator) Test(org.junit.Test)

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 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 DependantValue (org.hibernate.mapping.DependantValue)1