我有三个表和 2 个 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
最佳答案
我希望这会奏效,但不确定。请不要否定:)。我自己还没有尝试过。只是想出这个,所以它可能会对你有所帮助。干杯。
from Unit as units
inner join fetch units.unitGroups grp
inner join fetch grp.units
where grp.ugKey = 'amount' and units.id = 10
关于java - hibernate 多对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1672263/