简体   繁体   English

是否可以在没有迭代的情况下加载具有所有惰性属性的对象?

[英]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(..) ,但是您也可以序列化对象(使用ObjectOutputStreamXMLEncoder 。序列化机制将迭代所有属性并将其初始化。您可以使用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.

相关问题 是否可以在Java中动态设置对象属性(无反射)? - Is it possible to set object properties dynamically in Java (without reflection)? 没有惰性对象的休眠列表 - Hibernate List without Lazy Object 是否可以动态加载包中包含的所有属性文件? 即MyClass.class.getResource('*。properties'); - Is it possible load all properties files contained in a package dynamically? i.e. MyClass.class.getResource('*.properties'); 延迟加载不起作用-我只需要父对象,而不是所有关联 - Lazy load doesn't work - I Only need the parent object, not all associations Java 8在一次迭代中求和两个对象属性 - Java 8 Sum two object properties in one iteration 使用Java 8流对对象属性进行迭代 - Iteration over object Properties using Java 8 stream 使用冬眠/春天一次加载所有懒惰的集合 - Load all lazy collection at once with hibernate/spring 是否可以将CustomObject []转换为String []而无需迭代? - Is it possible to convert CustomObject[] to String[] without iteration? 如何在不列出所有属性的情况下基于注解通过mybatis将一个对象(超过10个属性)插入到mysql中 - How to insert an object(more than 10 properties) into mysql via mybatis based on annotation without list all properties Solr是否可以在没有Solr重启的情况下加载新的log4j.properties配置文件? - Is it possible for Solr to load a new log4j.properties configuration file without Solr restart?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM