我有三个表和两个JPA模型类:
Unit
------------
id [PK] - Integer
code - String
unitGroups - List<UnitGroup>
UnitGroup
------------
id [PK] - Integer
ugKey - String
units - List<Unit>
units和unitGroups之间有多对多的关系。 简单地说,我想编写一个HQL查询来获取以下sql的输出:
SELECT u.*
FROM units u, unit_groups ug, unit_group_pairs ugp
WHERE ugp.UnitID = u.ID
AND ugp.UnitGroupID = ug.ID
AND ug.UGKey = 'amount' AND u.ID = 10
答案 0 :(得分:1)
我希望这会奏效,但不确定。请不要否定:)。我自己没试过这个。想出这个,所以它可能对你有所帮助。欢呼声。
from Unit as units
inner join fetch units.unitGroups grp
inner join fetch grp.units
where grp.ugKey = 'amount' and units.id = 10
答案 1 :(得分:1)
最后:
select u from Unit u left join u.unitGroups ug where u.id = 10 and ug.ugKey = 'amount'
答案 2 :(得分:0)
试试这个
select u from unit as u
where u.ID = 10 and
'amount' = any elements(u.unitGroups.UGKey)