在 hql 中加入 Where 子句
Join with Where clause in hql
我的 sql 查询是
select count(tid) from admin.details tcd
inner join admin.module tm on tcd.moduleid=tm.module where
tm.bankid=1234 and roundid=2 and tm.status=true
如何使用 hql 获得相同的输出?
我试过了。我收到语法错误
select count(tid) from Details d join tcd.Module m
with d.moduleId=m.moduleId
where d.bankDetails.bankId = :bid
and d.roundMaster.roundId = :rid and m.status=:pid
您的 hql 查询中未定义 tcd 和 tid。
我认为你应该将你的 hql 建立在适当的实体上,这样任何连接都会自动到达。
无论如何,在实体中定义了正确的@joinColumn 属性之后,例如在 Detail 实体中应该有类似
的内容
@JoinColumn(name = "Detail_Module_Id", referencedColumnName = "Module_Id")
private Module moduleId;
你的 hql 查询应该变成:
select count(tid) from Details d
where d.bankDetails.bankId = :bid
and d.roundMaster.roundId = :rid
and d.moduleId.status= :pid
我不知道 d.bankDetails.bankId 和 d.roundMaster.roundId 是否可行,这取决于您的实体是如何为这些列定义的。
另一种方法是使用hql原生查询,但我不推荐。
我的 sql 查询是
select count(tid) from admin.details tcd
inner join admin.module tm on tcd.moduleid=tm.module where
tm.bankid=1234 and roundid=2 and tm.status=true
如何使用 hql 获得相同的输出?
我试过了。我收到语法错误
select count(tid) from Details d join tcd.Module m
with d.moduleId=m.moduleId
where d.bankDetails.bankId = :bid
and d.roundMaster.roundId = :rid and m.status=:pid
您的 hql 查询中未定义 tcd 和 tid。 我认为你应该将你的 hql 建立在适当的实体上,这样任何连接都会自动到达。
无论如何,在实体中定义了正确的@joinColumn 属性之后,例如在 Detail 实体中应该有类似
的内容@JoinColumn(name = "Detail_Module_Id", referencedColumnName = "Module_Id")
private Module moduleId;
你的 hql 查询应该变成:
select count(tid) from Details d
where d.bankDetails.bankId = :bid
and d.roundMaster.roundId = :rid
and d.moduleId.status= :pid
我不知道 d.bankDetails.bankId 和 d.roundMaster.roundId 是否可行,这取决于您的实体是如何为这些列定义的。 另一种方法是使用hql原生查询,但我不推荐。