I want to use a custom function in my HQL query, and I register a function in my database and this is my function's sql code:
BEGIN
DECLARE user_id_var VARCHAR(64);
SELECT
e.username
FROM
users e where e.id=30 INTO user_id_var;
return user_id_var;
END
and register it with a class as MysqlCustomDilect :
public class MysqlCustomDilect extends MySQLDialect{
public MysqlCustomDilect() {
super();
registerFunction("getActiveUser", new StandardSQLFunction("getActiveUser"));
}
}
and add it with this line to hibernate.cfg.xml file:
<property name="hibernate.dialect" value="myProject.common.MysqlCustomDilect" />
and call it like this code in my dao layer :
@Override
public List<Entity> getAll() {
Session session = getSession();
String hql = " select e.id as id,function('getActiveUser') as name from " + domainClass.getName() + " e ";
Query query=session.createQuery(hql);
return query.list();
}
but hibernate does not know it and raise this error :
unexpected token: function near line 1, column 18 [ from e.id as id,function('getActiveUser') ........