tags:

views:

82

answers:

2

I am trying call a db-function from HQL. The HQL statement should just call the function and return its value, like this

select someFunction(:someParameter)

If i try to call select current_timestamp()

it fails with

NHibernate.Hql.Ast.ANTLR.QuerySyntaxException: Exception of type 'Antlr.Runtime.MismatchedTreeNodeException' was thrown. near line 1, column 24 [select current_timestamp()]

I know that there is not much reason for retrieving the current timestamp. But a have created a few user-defined db-function that i would like to unit-test by calling them from HQL.

It seems to me that it's not possible to write a HQL statement without a FROM and WHERE clause. Can this be true?

IQuery query = unitOfWork.Session.CreateQuery("select current_timestamp()");

var ts = query.UniqueResult();

A: 
  1. It doesn't make much sense to call a function like that. Either you have a stored procedure which should be called using other methods, or you have a function which is not related to the DB at all, and therefore should not be running via the database to begin with.

  2. In any case, if the function doesn't work on a set of rows, can't you just add some dummy FROM clause, for example on a small static table?

Eldad Mor
1. I agree. I only do it to test the functions.
Søren Randrup
2. I could add a dummy FROM clause. The downside is that the test then depends on some dummy entity mapping.
Søren Randrup
+2  A: 

This could work if you explicitly make it an SQL Query, not an HQL query:

var query = unitOfWork.Session.CreateSQLQuery("select current_timestamp()");
Rafael Belliard