简体   繁体   English

休眠:更新以从其他表求和

[英]Hibernate: update to sum from other table

I'm trying to replicate this query from MySQL to HQL: UPDATE users u, mines m SET u.mana = u.mana + COALESCE((SELECT SUM(m.mana_rate) FROM mines m WHERE m.user_id = u.id), 0) 我正在尝试将此查询从MySQL复制到HQL: UPDATE用户u,地雷m SET u.mana = u.mana + COALESCE((SELECT SUM(m.mana_rate)FROM mines m WHERE m.user_id = u.id) ,0)

Simply doing this in HQL: update User u set u.mana = u.mana + (select coalesce(sum(m.manaRate), 0) from Mine m where m.userId = u.id) 只需在HQL中执行此操作即可: 更新用户u设置u.mana = u.mana +(从Mine m中选择coalesce(sum(m.manaRate),0),其中m.userId = u.id)

Gives following error: Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: query [update mm.user.User u set u.mana = u.mana + (select coalesce(sum(m.manaRate), 0) from mm.map.buildings.mines.Mine m where m.userId = u.id)] 给出以下错误: 线程“ main”中的异常org.hibernate.hql.ast.QuerySyntaxException:意外的AST节点:查询[更新mm.user.User设置了u.mana = u.mana +(选择Coalesce(sum(m。 manaRate),0)from mm.map.buildings.mines.Mine m,其中m.userId = u.id)]

I'm new to Hibernate, so I don't really know where to start, and what the error means. 我是Hibernate的新手,所以我真的不知道从哪里开始以及错误的含义。 If it's not possible, can i write a native MySQL query in Hibernate? 如果不可能,我可以在Hibernate中编写本机MySQL查询吗?

EDIT: The problem seems to be in "u.mana = u.mana +", because when I just do u.mana = (select... the query works just fine 编辑:问题似乎出在“ u.mana = u.mana +”,因为当我只做u.mana =(选择...时,查询工作正常

you can write native queries in hibernate, more about that here . 您可以在休眠状态下编写本机查询,有关更多信息,请参见此处 bit sure what is wrong with your query though. 有点确定您的查询有什么问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM