HQL:从Eager Table获取加入集合 [英] HQL: Fetch Join Collections from Eager Table

查看:103
本文介绍了HQL:从Eager Table获取加入集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个表:

  RootNode //将返回多个根节点
SubNode //将返回一个每个根节点的子节点
SubNodeChildren1 //将为每个子节点返回多个
SubNodeChildren2 //将为每个子节点返回多个

以及类似的实体结构:

  RootNode  - >子节点 - > SubNodeChildren1 
- > SubNodeChildren2

我需要一个查询来返回所有的 RootNodes SubNode 和 SubNode 子进行了初始化。 子节点被热切地抓取,但是子节点子节点被抓取。



我知道如何编写一个查询,它将

$ L code> LEFT OUTER JOIN FETCH 一个表的直接子元素并相应地初始化它们。但是,我不知道如何获取从顶级表中急切获取的表的子元素。



我尝试过类似的方法:

 选择从根节点到左外部连接获取rn.SubNode.SubNodeChildren1 

但是,这总是给我一个错误,即所有者不属于SELECT。



任何帮助非常感谢。

解决方案

这里介绍Hibernate参考



lockquote

我们可能需要一个别名的唯一原因是,如果我们递归地获取更多集合


这意味着您的查询应该被重写为

 选择不同的
rn
from
RootNode rn
left join fetch
rn.subNode sn
left join fetch
sn.subNodeChildren

您可以

禁用默认的子节点fetch = FetchType.EAGER,并通过使用HQL查询来检索你真正想要的东西 - 它(HQL查询)有效地覆盖了关联和集合的映射文件的外连接和惰性声明 Hibernate参考文档)。 POJO在Action书中支持这种方法。

或者启用SubNodeChildren的集合作为fetch = FetchType.EAGER

以下内容已经从Hibernate常见问题(链接已被禁用(据我所知),但我已经保存之前消失)



在MVC应用程序中,我们如何确保所有代理和懒惰集合将在视图尝试访问它们时被初始化?


一种可能的方法是将会话保持打开状态(和事务未提交)转发到视图时。会话/事务将在例如servlet过滤器(例如使用Maverick中的ModelLifetime.discard()回调)中呈现视图之后关闭/落实。这种方法的一个难点是确保会话/事务关闭/回滚,如果渲染视图出现异常。


.. 。


另一种方法是使用Hibernate.initialize()强制初始化所有需要的对象。这通常比听起来更直接。



I have four tables:

RootNode // Will return multiple root nodes 
SubNode // Will return one sub node per root node
SubNodeChildren1 // Will return multiple for each sub node
SubNodeChildren2 // Will return multiple for each sub node

and a similar entity structure:

RootNode -> SubNode -> SubNodeChildren1
                    -> SubNodeChildren2

I need one query that will return all the RootNodes in the table with its SubNode and SubNode children initialized. The SubNode is eagerly fetched, but the SubNode children is lazy fetched.

I know how to write a query that will LEFT OUTER JOIN FETCH the immediate children of a table and initialize them accordingly. However, I have no idea of how to grab the children of a table that is eagerly fetched from the top-level table.

I have tried something like:

SELECT rn FROM RootNode AS rn LEFT OUTER JOIN FETCH rn.SubNode.SubNodeChildren1

but, this always gives me an error that the owner is not part of the SELECT.

Any help is greatly appreciated.

解决方案

Here goes Hibernate reference

The only reason we might need an alias is if we are recursively join fetching a further collection

Which implies your query should be re-written as

select distinct 
    rn
from
    RootNode rn
left join fetch 
    rn.subNode sn
left join fetch 
    sn.subNodeChildren

You can either

disable default subNode fetch=FetchType.EAGER and just retrieve what you really want by using HQL query - It (HQL query) effectively overrides the outer join and lazy declarations of the mapping file for associations and collections (Hibernate reference documentation). This approach is supported by POJO in Action book.

or enable the collection of SubNodeChildren as fetch=FetchType.EAGER

The following has been extracted from the Hibernate FAQ (The link has been disabled (As far as i know) but i have saved before disappearing)

In an MVC application, how can we ensure that all proxies and lazy collections will be initialized when the view tries to access them ?

One possible approach is to leave the session open (and transaction uncommitted) when forwarding to the view. The session/transaction would be closed/committed after the view is rendered in, for example, a servlet filter (another example would by to use the ModelLifetime.discard() callback in Maverick). One difficulty with this approach is making sure the session/transaction is closed/rolled back if an exception occurs rendering the view.

...

Another approach is to simply force initialization of all needed objects using Hibernate.initialize(). This is often more straightforward than it sounds.

这篇关于HQL:从Eager Table获取加入集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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