子查询中的hibernate访问外部查询字段

时间:2018-02-28 22:03:02

标签: mysql sql hibernate

我有以下查询在Mysql Workbench中有效但在hibernate中出错

insert into namingsequences(currentseq, namingtype, scope, sequencekey, startrange, endrange, steprange, rangeactive)        
   select (a.currentseq + 5) as currentseq, namingtype, scope, sequencekey,               startrange, endrange, steprange, rangeactive from namingsequences a
    where a.sequencekey = 'test1123' and a.namingtype = 'test' and a.scope = 'test' 
     and a.currentseq >= a.startrange and a.currentseq <= a.endrange and not exists
     (select 1 from namingsequences b where b.sequencekey = 'test1123' and b.namingtype = 'test' 
      and b.currentseq >= b.startrange and b.currentseq <= b.endrange
      and b.scope = 'test' and b.currentseq = (a.currentseq+5) )
      order by currentseq ;

    insert into NamingSequences ( namingtype, scope, sequencekey,startrange,                         
    endrange, steprange, currentseq, rangeactive ) 
    select namingsequ0_.namingtype as col_0_0_, namingsequ0_.scope as col_1_0_,        
    namingsequ0_.sequencekey as col_2_0_, namingsequ0_.startrange as col_3_0_, 
    namingsequ0_.endrange as col_4_0_, namingsequ0_.steprange as col_5_0_, 
    namingsequ0_.currentseq+? as col_6_0_, namingsequ0_.rangeactive as  col_7_0_ from NamingSequences namingsequ0_ 
    where namingsequ0_.sequencekey=? and namingsequ0_.namingtype=? and namingsequ0_.scope=? 
    and namingsequ0_.currentseq>=namingsequ0_.startrange and namingsequ0_.currentseq<=namingsequ0_.endrange 
    and  not (exists (select 1 from NamingSequences namingsequ1_ where 
     namingsequ1_.sequencekey=? and namingsequ1_.namingtype=? 
     and  namingsequ1_.currentseq>=namingsequ1_.startrange 
     and namingsequ1_.currentseq<=namingsequ1_.endrange 
     and namingsequ1_.scope=? and   
     namingsequ1_.currentseq=NamingSequences.currentseq+?)) 
     order by namingsequ0_.currentseq

[DEV:2018-Feb-28 16:32:47,884] [WARN] [http-nio-8082-exec-35] SQL错误:1054,SQLState:42S22

[DEV:2018-Feb-28 16:32:47,884] [ERROR] http-nio-8082-exec-35未知栏&#39; NamingSequences.currentseq&#39; 在& #39; where where&#39;

它似乎无法从子查询访问外部查询字段。该错误似乎来自hibernate查询倒数第二行。 另外,我可以限制select并插入只返回一行吗?

Query hbquery = session.createQuery(queryString);
hbquery.setMaxResults(1);
hbquery.setParameter("scope", scope);
hbquery.setLong("increment", step);
hbquery.setParameter("nameType", namingType);
hbquery.setParameter("seqKey", seqKey);
int ret = hbquery.executeUpdate();

0 个答案:

没有答案