HQL不接受使用join的更新SQL

时间:2014-08-04 03:35:02

标签: java mysql sql hibernate hql

我需要在我的java应用程序中将此SQL运行到MySQL数据库:

update group_table 
join
  (
        select tab1.letter, tab1.id_group_table from 
        (
          select  letter,
          id_group_table, 
          count(letter) as occurrences
          from letter
          group by id_group_table, letter 
          order by occurrences desc
        ) tab1
        group by tab1.id_group_table having max(tab1.occurrences)    

  ) tab2 on group_table.id_group_table = tab2.id_group_table

set champion = tab2.letter
where group_table.id_whatever in (1,2,3,4);

如果我在数据库中尝试它,它会起作用。现在,我正在尝试使用hibernate:

String hqlUpdate = "update group_table join (select tab1.letter, tab1.id_group_table from (select  letter,id_group_table, count(letter) as occurrences from letter group by id_group_table, letter order by occurrences desc) tab1 group by tab1.id_group_table having max(tab1.occurrences)) tab2 on group_table.id_group_table = tab2.id_group_table set champion = tab2.letter where group_table.id_whatever in (1,2,3,4);";
getSession().createQuery( hqlUpdate ).executeUpdate();

这是我得到的错误:

Ago 04, 2014 12:16:18 AM org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR: line 1:38: expecting "set", found 'JOIN'
line 1:38: expecting "set", found 'JOIN'
    at antlr.Parser.match(Parser.java:211)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.setClause(HqlBaseParser.java:414)

我需要运行这个确切的查询... 如何使用hibernate

谢谢!

2 个答案:

答案 0 :(得分:2)

尝试以下代码

String hqlUpdate = "update group_table join (select tab1.letter, tab1.id_group_table from (select  letter,id_group_table, count(letter) as occurrences from letter group by id_group_table, letter order by occurrences desc) tab1 group by tab1.id_group_table having max(tab1.occurrences)) tab2 on group_table.id_group_table = tab2.id_group_table set champion = tab2.letter where group_table.id_whatever in (1,2,3,4);";     
getSession().createSQLQuery( hqlUpdate ).executeUpdate();

答案 1 :(得分:1)

如果您需要确切的查询,请不要使用HQL,而是使用native query support