0

我正在运行一个具有正确连接的 hql:

select sum(banks.money) from banks right join banks.account where banks.account = :account and (banks.date < :date or banks.date is null) group by banks.account, banks.date

hibernate 把它翻译成这样的:

select sum(banks.money) from banks right join accounts on banks.account_id = accounts.account_id where banks.account_id = someId and (banks.date < someDate or banks.date is null) ....

我希望休眠从 accounts 表中获取 account_id 字段(我可以有一个不在银行表中的帐户)而不是从银行表中获取。

有没有办法明确告诉休眠在连接表的哪一列上执行?

4

1 回答 1

0

格式化有帮助...无论如何...

select sum(banks.money) 
from banks 
    right join banks.account acct 
where acct = :account 
  and (banks.date < :date or banks.date is null) 
group by banks.account, banks.date

虽然就个人而言,我会将其添加到加入条件中:

select sum(banks.money) 
from banks 
    right join banks.account acct 
        with acct.id = :accountId
where banks.date < :date 
   or banks.date is null
group by banks.account, banks.date

在这两种情况下,请特别注意给予加入关联的别名及其作为限定符的用途

于 2012-09-26T15:18:49.110 回答