【发布时间】:2010-08-18 20:56:09
【问题描述】:
我有一个用户表,其中包含两种不同类型的用户(A 和 B)。 A 类用户可以与 B 类用户建立关系(记录在关系表中)。 A 类用户可以与 B 类用户建立的关系数记录在另一个表的一个字段中。
我想让 Hibernate 返回所有未建立所有关系的 A 类用户。我有一个 SQL 查询可以做到这一点。我需要做什么?
编辑
我已经设法通过将我的查询放入 session.createSQLQuery() 中来完成这项工作,但是在所有这些中肯、令人敬畏的休眠代码中包含一个 SQL 块似乎很奇怪。我想,如果可能的话,我宁愿把它放在 HSQL 或 Hibernate 对象中。
这是我试图转换为 HSQL 的伪查询:
select userId
from (
select user_id, max_mentees, count(connection_id) num_mentees
from user_table
join mentor on mentor_id = user_id and user_status = 'ACTIVE'
left join connections on connection_mentor_id = mentor_id
and connection_status = 'ACTIVE' and connection_end > sysdate
group by user_id, max_mentees, connection_id
)
where (max_mentees > num_mentees)
group by user_id
【问题讨论】:
-
I want to have Hibernate return all type A users that do not have all of their relationships established什么时候?什么时候做什么? -
基本上,A 用户将成为 B 用户的导师,而 A 用户一次可以拥有多少“导师”是有限制的。计数是看有多少B(被指导者)用户分配给了A(指导者)用户,以便我们可以将其与另一个表中的限制数量进行比较。