休眠条件和多重连接 [英] Hibernate Criteria and multiple join

查看:29
本文介绍了休眠条件和多重连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用 Hibernate 标准可以做到吗?

is possible with Hibernate criteria do it?

select A.something, B.something, C.something, D.something
    from  A JOIN B on A.id = B.id_fk
          JOIN C ON B.id = C.id_fk
          JOIN D ON C.id = D.id_fk;

推荐答案

我遇到了完全相同的问题,并且能够像这样解决它:

I have got exactly the same problem, and was able to resolve it like this:

return criteria.createCriteria(A.class)
               .createCriteria("b", "join_between_a_b")
               .createCriteria("c", "join_between_b_c")
               .createCriteria("d", "join_between_c_d")
               .add(Restrictions.eq("some_field_of_D", someValue));

注意:上面代码中的"b""c""d"是指A<中的属性名/code>、BC 类,相应地(A 类具有属性 b 等).

Note: "b", "c" and "d" in code above refer to attribute names in A, B and C classes, correspondingly (class A has attribute b and so on).

对于此解决方案,您甚至不需要在 A.hbm.xml 中设置 lazyfetch 参数.

For this solution you don't even need to have lazy and fetch parameters to be set in your A.hbm.xml.

这篇关于休眠条件和多重连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆