休眠多对多

wgxvkvu9  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(330)

我有三个表和两个jpa模型类:

Unit
------------
id [PK]    - Integer
code       - String
unitGroups - List<UnitGroup>

UnitGroup
------------
id [PK]    - Integer
ugKey      - String
units      - List<Unit>

单元和单元组之间存在多对多关系。简单地说,我想编写一个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
eh57zj3b

eh57zj3b1#

试试这个

select u from  unit as u 
where u.ID = 10 and 
'amount' = any elements(u.unitGroups.UGKey)
gajydyqb

gajydyqb2#

我希望这能奏效,但不确定。请不要否定:)。我自己还没试过。想出这个,这样可能会对你有帮助。干杯。

from Unit as units 
inner join fetch units.unitGroups grp
inner join fetch grp.units
where grp.ugKey = 'amount' and units.id = 10
gt0wga4j

gt0wga4j3#

最后:

select u from Unit u left join u.unitGroups ug where u.id = 10 and ug.ugKey = 'amount'

相关问题