how to use string left function in hql

北城余情 提交于 2019-12-20 02:37:21

问题


I have a sql query like this

select column from table where path = left('INPUTSTRING', length(path));

and trying to accomplish it in hql like this,

  return session.createQuery("from Table where Path = left(:input, length(Path))").
                            query.setParameter("input", inputPath).
                            .list();

and getting an error like this

Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: left near line 1

how to get this done? What is the corresponding string function in hql? Is there a solution for this using criteria query apis?


回答1:


Yes, left() is not supported by the MySQLDialect. See the list of HQL supported functions on API docs.

Now you have left with 2 options.

  1. Use session.createSQLQuery() method.
  2. Create Your own Dialect class by extending the MySQLDialect and register the function there. This is told at hibernate forum here explained well in a blog post here.



回答2:


I'm not sure if HQL does this for you , but you can use IQuery/session.CreateSQLQuery() to use a raw SQL query to populate a mapped entity. I've never used it for substrings, but have used it for aggregate functions. Check chapter 13 of the NHibernate docs and see if that does it for you. You can check the query substitution available in Nhibernate - here



来源:https://stackoverflow.com/questions/9634338/how-to-use-string-left-function-in-hql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!