如何在hql中使用字符串左功能 [英] how to use string left function in hql

查看:134
本文介绍了如何在hql中使用字符串左功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 从表中选择列where path = left('INPUTSTRING',length(path )); 

并试图在hql中完成它,

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

并得到这样的错误

 原因:org.hibernate.hql.ast.QuerySyntaxException:意外的令牌:在第1行左边

如何完成这项工作? hql中对应的字符串函数是什么?是否有解决方案使用标准查询apis?

解决方案

是, left()不被 MySQLDialect 支持。请参阅 API上HQL支持的函数列表docs



现在您剩下2个选项。


  1. 使用 session.createSQLQuery()方法

  2. 创建您自己的 Dialect 类通过扩展 MySQLDialect 并在那里注册函数。这是在hibernate论坛这里讲述的,在博客文章中有很好的解释此处


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?

解决方案

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.

这篇关于如何在hql中使用字符串左功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆