Search in sources :

Example 6 with SqlFunction

use of org.hibernate.dialect.function.SqlFunction 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 SqlFunction

use of org.hibernate.dialect.function.SqlFunction in project hibernate-orm by hibernate.

the class FunctionNameAsColumnTest method testGetMultiColumnSameNameAsNoArgFunctionCriteria.

@Test
public void testGetMultiColumnSameNameAsNoArgFunctionCriteria() {
    SQLFunction function = sessionFactory().getSqlFunctionRegistry().findSQLFunction("current_date");
    if (function == null || function.hasParenthesesIfNoArguments()) {
        SkipLog.reportSkip("current_date reuires ()", "tests noarg function that does not require ()");
        return;
    }
    Session s = openSession();
    Transaction t = s.beginTransaction();
    EntityWithNoArgFunctionAsColumn e1 = new EntityWithNoArgFunctionAsColumn();
    e1.setCurrentDate("blah blah blah");
    EntityWithNoArgFunctionAsColumn e2 = new EntityWithNoArgFunctionAsColumn();
    e2.setCurrentDate("yadda yadda yadda");
    EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder();
    holder1.getEntityWithNoArgFunctionAsColumns().add(e1);
    EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder();
    holder2.getEntityWithNoArgFunctionAsColumns().add(e2);
    holder1.setNextHolder(holder2);
    s.save(holder1);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    holder1 = (EntityWithFunctionAsColumnHolder) s.createCriteria(EntityWithFunctionAsColumnHolder.class).add(Restrictions.isNotNull("nextHolder")).setFetchMode("entityWithNoArgFunctionAsColumns", FetchMode.JOIN).setFetchMode("nextHolder", FetchMode.JOIN).setFetchMode("nextHolder.entityWithNoArgFunctionAsColumns", FetchMode.JOIN).uniqueResult();
    assertTrue(Hibernate.isInitialized(holder1.getEntityWithNoArgFunctionAsColumns()));
    assertTrue(Hibernate.isInitialized(holder1.getNextHolder()));
    assertTrue(Hibernate.isInitialized(holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns()));
    assertEquals(1, holder1.getEntityWithNoArgFunctionAsColumns().size());
    e1 = (EntityWithNoArgFunctionAsColumn) holder1.getEntityWithNoArgFunctionAsColumns().iterator().next();
    assertEquals("blah blah blah", e1.getCurrentDate());
    assertEquals(1, holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns().size());
    e2 = (EntityWithNoArgFunctionAsColumn) (holder1.getNextHolder()).getEntityWithNoArgFunctionAsColumns().iterator().next();
    assertEquals("yadda yadda yadda", e2.getCurrentDate());
    t.commit();
    s.close();
    cleanup();
}
Also used : Transaction(org.hibernate.Transaction) SQLFunction(org.hibernate.dialect.function.SQLFunction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 8 with SqlFunction

use of org.hibernate.dialect.function.SqlFunction in project hibernate-orm by hibernate.

the class HQLTest method testExpressionInFunction.

@SuppressWarnings({ "unchecked" })
@Test
public void testExpressionInFunction() throws Exception {
    assertTranslation("from Animal an where an.bodyWeight > abs(3-5)");
    assertTranslation("from Animal an where an.bodyWeight > abs(3/5)");
    assertTranslation("from Animal an where an.bodyWeight > abs(3+5)");
    assertTranslation("from Animal an where an.bodyWeight > abs(3*5)");
    SQLFunction concat = sessionFactory().getSqlFunctionRegistry().findSQLFunction("concat");
    List list = new ArrayList();
    list.add("'fat'");
    list.add("'skinny'");
    assertTranslation("from Animal an where an.description = " + concat.render(StringType.INSTANCE, list, sessionFactory()));
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) SQLFunction(org.hibernate.dialect.function.SQLFunction) Test(org.junit.Test)

Example 9 with SqlFunction

use of org.hibernate.dialect.function.SqlFunction in project hibernate-orm by hibernate.

the class QueryAndSQLTest method testNativeQueryWithFormulaAttribute.

@Test
public void testNativeQueryWithFormulaAttribute() {
    SQLFunction dateFunction = getDialect().getFunctions().get("current_date");
    String dateFunctionRendered = dateFunction.render(null, java.util.Collections.EMPTY_LIST, sessionFactory());
    String sql = String.format("select t.TABLE_NAME as {t.tableName}, %s as {t.daysOld} from ALL_TABLES t  where t.TABLE_NAME = 'AUDIT_ACTIONS' ", dateFunctionRendered);
    String sql2 = String.format("select TABLE_NAME as t_name, %s as t_time from ALL_TABLES   where TABLE_NAME = 'AUDIT_ACTIONS' ", dateFunctionRendered);
    Session s = openSession();
    s.beginTransaction();
    s.createSQLQuery(sql).addEntity("t", AllTables.class).list();
    s.createSQLQuery(sql2).setResultSetMapping("all").list();
    SQLQuery q = s.createSQLQuery(sql2);
    q.addRoot("t", AllTables.class).addProperty("tableName", "t_name").addProperty("daysOld", "t_time");
    q.list();
    s.getTransaction().commit();
    s.close();
}
Also used : SQLFunction(org.hibernate.dialect.function.SQLFunction) SQLQuery(org.hibernate.SQLQuery) Session(org.hibernate.Session) Test(org.junit.Test)

Example 10 with SqlFunction

use of org.hibernate.dialect.function.SqlFunction in project hibernate-orm by hibernate.

the class FunctionNameAsColumnTest method testGetMultiColumnSameNameAsNoArgFunctionHQL.

@Test
public void testGetMultiColumnSameNameAsNoArgFunctionHQL() throws Exception {
    SQLFunction function = sessionFactory().getSqlFunctionRegistry().findSQLFunction("current_date");
    if (function == null || function.hasParenthesesIfNoArguments()) {
        SkipLog.reportSkip("current_date reuires ()", "tests noarg function that does not require ()");
        return;
    }
    Session s = openSession();
    Transaction t = s.beginTransaction();
    EntityWithNoArgFunctionAsColumn e1 = new EntityWithNoArgFunctionAsColumn();
    e1.setCurrentDate("blah blah blah");
    EntityWithNoArgFunctionAsColumn e2 = new EntityWithNoArgFunctionAsColumn();
    e2.setCurrentDate("yadda yadda yadda");
    EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder();
    holder1.getEntityWithNoArgFunctionAsColumns().add(e1);
    EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder();
    holder2.getEntityWithNoArgFunctionAsColumns().add(e2);
    holder1.setNextHolder(holder2);
    s.save(holder1);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    holder1 = (EntityWithFunctionAsColumnHolder) s.createQuery("from EntityWithFunctionAsColumnHolder h left join fetch h.entityWithNoArgFunctionAsColumns " + "left join fetch h.nextHolder left join fetch h.nextHolder.entityWithNoArgFunctionAsColumns " + "where h.nextHolder is not null").uniqueResult();
    assertTrue(Hibernate.isInitialized(holder1.getEntityWithNoArgFunctionAsColumns()));
    assertTrue(Hibernate.isInitialized(holder1.getNextHolder()));
    assertTrue(Hibernate.isInitialized(holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns()));
    assertEquals(1, holder1.getEntityWithNoArgFunctionAsColumns().size());
    t.commit();
    s.close();
    e1 = (EntityWithNoArgFunctionAsColumn) holder1.getEntityWithNoArgFunctionAsColumns().iterator().next();
    assertEquals("blah blah blah", e1.getCurrentDate());
    assertEquals(1, holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns().size());
    e2 = (EntityWithNoArgFunctionAsColumn) (holder1.getNextHolder()).getEntityWithNoArgFunctionAsColumns().iterator().next();
    assertEquals("yadda yadda yadda", e2.getCurrentDate());
    cleanup();
}
Also used : Transaction(org.hibernate.Transaction) SQLFunction(org.hibernate.dialect.function.SQLFunction) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

SQLFunction (org.hibernate.dialect.function.SQLFunction)18 Test (org.junit.Test)7 List (java.util.List)5 ArrayList (java.util.ArrayList)4 Session (org.hibernate.Session)4 Type (org.hibernate.type.Type)4 Map (java.util.Map)3 Transaction (org.hibernate.Transaction)3 CommonAST (antlr.CommonAST)2 AST (antlr.collections.AST)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Mapping (org.hibernate.engine.spi.Mapping)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 FunctionNode (org.hibernate.hql.internal.ast.tree.FunctionNode)2 Component (org.hibernate.mapping.Component)2 Formula (org.hibernate.mapping.Formula)2 TestForIssue (org.hibernate.testing.TestForIssue)2 HibernateException (org.hibernate.HibernateException)1 SQLQuery (org.hibernate.SQLQuery)1