java 休眠延迟加载不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5180526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate lazy loading not working
提问by Farouk Alhassan
I'm using version 3.6.1.Final
我使用的是 3.6.1.Final 版本
I have the following property in my entity bean
我的实体 bean 中有以下属性
@JoinColumn( name = "FOLDER_PARENT_ID", referencedColumnName = "FOLDER_ID" )
@ManyToOne(cascade=CascadeType.MERGE, fetch= FetchType.LAZY )
private FolderTbl parent;
In my unit test, Assertnull fails because getParent() is not null
在我的单元测试中,Assertnull 失败,因为 getParent() 不为 null
assertNull( folderTbl.getParent() );
What else do I have to do to stop hibernate loading the parent?
我还需要做什么来停止休眠加载父级?
采纳答案by Arun P Johny
Even if you set the lazy to true, the parent value will not be null. The lazy load uses a proxy object and assign it to the parent property. When we try to use the parent(call getParent()
) it will load the actual parent object using the proxy object.
即使将lazy设置为true,父值也不会为null。延迟加载使用代理对象并将其分配给父属性。当我们尝试使用 parent(call getParent()
) 时,它将使用代理对象加载实际的父对象。
If you do not want to load the object do not configure the JPA properties for the item and set it as transient.
如果您不想加载对象,请不要为该项目配置 JPA 属性并将其设置为瞬态。
回答by Jigar Joshi
Parent is configured correctlyto load lazily, the point is you are testing it wrongly.
Parent 被正确配置为延迟加载,关键是你测试它是错误的。
Hibernate will load the object when you invoke the method getParent()
, when request to actual object comes it will load .
当您调用该方法时getParent()
,Hibernate 将加载该对象,当对实际对象的请求到来时,它将加载。
You can check this thing by configuring show_sql
to true. it will invoke a query when you invoke getParent()
你可以通过配置show_sql
为true来检查这件事。它会在您调用时调用查询 getParent()
回答by ams
Hibernate treats the Lazy fetching as a hint. Here is what the JPA 2.0 spec says on page 364 Table 9.
Hibernate 将 Lazy fetching 视为一种提示。这是 JPA 2.0 规范在第 364 页表 9 上所说的内容。
(Optional) Whether the value of the field or property should be lazily loaded or must be eagerly fetched. The EAGER strategy is a requirement on the persistence provider runtime that the value must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime.
(可选)字段或属性的值是应该延迟加载还是必须急切地获取。EAGER 策略是对持久性提供程序运行时的一项要求,即必须急切地获取值。LAZY 策略是对持久性提供程序运行时的提示。
回答by Vinod R
For some scenarios you might need not load the lazy collection at all. You can have a method like below to detach the collection from the session.
对于某些情况,您可能根本不需要加载惰性集合。您可以使用如下方法将集合与会话分离。
public class. .... {
.....
@JoinColumn( name = "FOLDER_PARENT_ID", referencedColumnName = "FOLDER_ID" )
@ManyToOne(cascade=CascadeType.MERGE, fetch= FetchType.LAZY )
private FolderTbl parent;
...
public void detachLazyObjects() {
parent = null;
}
Then call this method to detach the class from where you need it to be null. Please use this shortcut sparingly, I would suggest you to think about other ways of doing it before resorting to this solution.
然后调用此方法将类从您需要它的位置分离为空。请谨慎使用此快捷方式,我建议您在使用此解决方案之前考虑其他方法。
回答by Thomas
Actually, invoking getParent() might return a proxy instance that shows you there is a parent. If you access fields other than the id the parent would be loaded if necessary.
实际上,调用 getParent() 可能会返回一个代理实例,显示您有一个父对象。如果您访问 id 以外的字段,则将在必要时加载父项。
Note that the parent might be loaded already by the transaction and thus reside in the first level cache. If so, Hibernate normally won't do another query to the database.
请注意,父级可能已被事务加载,因此驻留在一级缓存中。如果是这样,Hibernate 通常不会对数据库执行另一个查询。
As said before, if your entity has a non-transient reference to a parent getParent() will always return a non-null value, even if the parent itself is not loaded yet.
如前所述,如果您的实体具有对父级的非瞬态引用,则 getParent() 将始终返回非空值,即使父级本身尚未加载。