java - hibernate 多对多

标签 java sql hibernate hql

我有三个表和 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/

相关文章:

java - 错误使用 if 指令

java - 一个 MapReduce 程序的输出作为另一个 MapReduce 程序的输入

java - Java 中的类是否具有(唯一)标识?

java - 为什么 `exception` 在任何地方都看不到?

sql - 存储过程 T-SQL 中的递增/递减

java - 基于反射的基本通用 DAO

hibernate - GORM 组合 - 具有多对一关系的嵌入式域引发 org.hibernate.MappingException

sql - 在 Go 中控制指针

c# - SQL 查询通配符搜索

java - 使用 Hibernate 保留原始 Java Enum