[英]Is it possible to load object with all lazy properties without a iteration?
I have an object which has children, some of which loaded lazy some not. 我有一个带有子对象的对象,其中一些子对象加载了一些惰性对象,而有些子对象则没有。 Now I have situation where I need a parent object to load with all the child at once.
现在,我需要一个父对象同时加载所有子对象的情况。 Is there quick way to do this without checking
Hibernate.isInitialized()
on all the properties? 有没有一种快速的方法,而无需在所有属性上都检查
Hibernate.isInitialized()
?
Thank you 谢谢
我将使用Hibernate.initialze(..)
,但是您也可以序列化对象(使用ObjectOutputStream
或XMLEncoder
。序列化机制将迭代所有属性并将其初始化。您可以使用commons-lang SerializationUtils
。
How are you obtaining the object? 您如何获取对象?
A general approach to this (it's often called 'hydration' of the objects) is to ask for a join fetch. 一种通用方法(通常称为对象的“水合”)是要求进行联接提取。 That's where the ORM retrieves the data for the child objects at the same time as the data for the parent by using a join, and uses the data to eagerly initialize the children.
在那儿,ORM通过联接使用父对象的数据同时检索子对象的数据,并使用该数据热切地初始化子对象。
There are various ways to ask for a join fetch, mostly provider-specific, but you can write it into portable JPQL : 有多种方法可以请求联接获取,大多数情况下是特定于提供程序的,但是您可以将其写入可移植的JPQL中 :
select p from Parent p join fetch p.children where ...
However, note that this will return each parent multiple times - as many as it has children. 但是,请注意,此操作将多次返回每个父级-与其返回的子级一样多。 That's a bit weird, but it's how it works.
有点奇怪,但是它是这样工作的。
I agree with Tom, I use fetch joins to bring back child objects that I want initialized. 我同意Tom的观点,我使用访存联接来带回要初始化的子对象。 As far as the query bringing back multiple Parent objects, I believe adding a "DISTINCT" clause should fix that.
至于带回多个Parent对象的查询,我相信添加“ DISTINCT”子句应该可以解决该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.