一尘不染

遇到mysql:=运算符时发生休眠异常

hibernate

当我执行以下代码时,发生异常:

Exception: org.springframework.orm.hibernate3.HibernateQueryException: 
Not all named parameters have been set

这是我的代码:

queryString = SET @quot=0,@latest=0,@comp='';
    select B.* from (
    select A.time,A.change,IF(@comp<>A.company,1,0) as LATEST,@comp:=A.company as company from (
    select time,company,quote-@quot as `change`, @quot:=quote curr_quote
    from stocks order by company,time) A
    order by company,time desc) B where B.LATEST=1;

list = getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session)throws     HibernateException,SQLException {
        SQLQuery  query = session.createSQLQuery(queryString);
        query.setParameterList("list", custIds);
        return query.list();
    }

这种行为的原因是什么?


阅读 198

收藏
2020-06-20

共1个答案

一尘不染

这是一个有点费解,究竟是什么您正在执行的查询,但如果你需要使用冒号在本地查询,你的情况为“赋值”操作符,你应该逃避所有的结肠occurances
\\在您的带有查询的Java字符串,因此可能类似于:

select B.* from (
  select A.time,A.change,IF(@comp<>A.company,1,0) as LATEST,@comp\\:=A.company as company from (
      select time,company,quote-@quot as `change`, @quot\\:=quote curr_quote
      from stocks order by company,time) A
    order by company,time desc) B where B.LATEST=1;

更新:
似乎,尚无法在Hibernate本机查询中转义冒号,这是一个未解决的问题。这意味着,您不能在Hibernate本机查询中使用冒号,而不用于命名参数。您可以尝试创建一个函数并调用它,而不是调用查询。

2020-06-20