如何在hibernate中构建分层查询?

时间:2017-03-29 13:37:14

标签: hibernate hierarchy

您好我有以下数据模型(简化):

User
   Long getDelegationId();

Expedient
   Long getDelegationId();

Delegation
    Long getId();
    Set<Delegation> getSubDelegations();
    Delegation getParentDelegation();

现在我需要创建一个过滤器,这样用户只能看到属于他的代表团的权宜之计或者它的subdelegations最大深度为3 lvl。

我怎样才能做到这一点?

我尝试以下查询:

from Expedient aliasExpedient where aliasExpedient.delegation.ID in 
( select delegation4.ID from Delegation as delegation1
join fetch delegation1.delegations as delegation2
join fetch delegation2.delegations as delegation3
join fetch delegation3.delegations as delegation4
where delegation1.ID = ?)

包含fetch,我得到:

org.hibernate.QueryException: fetch not allowed in subquery from-elements

没有它我没有结果。

有关如何实现目标的任何提示?

谢谢, 贡萨洛

0 个答案:

没有答案