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));
}
}
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();
}
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()));
}
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();
}
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();
}
Aggregations