问题描述
我有一个JPA设置,如果我不使用延迟加载,几乎整个数据库都会被加载。我也直接在模型上使用序列化,所以有时我需要初始化代理。
我只想对集合使用延迟加载。事实上,一些奇异的实体被热切地提取工作得很好。但是,无论我如何设置集合,我都从未得到代理集合,我总是得到完全加载的集合。
这是一些示例代码:
@Entity
公共类Thread实现Externalizable {
@OneToMany(mappedBy =parentThread,fetch = FetchType。 LAZY)
public List< Reply> getReplies(){
返回答案;
$ b 所以这里的问题是当我检查调试器时,persistantBag-list的答复总是充满信息,并且是实际的答复对象,而不是我想要的空代理。
我使用entityManager.find(Thread.class,ID),当我想要线程和像这样的所有集合总是完全加载,无论我有fetch = FetchType.LAZY或不。
据我所知,设置fetchtype懒惰应该按我的意愿工作。另外,我希望在使用thread.getReplies()时加载实体,以便序列化并将它们发送到客户端。我不知道getReplies是否可以与代理实体一起工作,因为我从来没有收集到任何延迟加载的集合。
一边不使用Intellij并用它来设置JPA与休眠。
我也问过一个类似的问题,我希望这个集合完全是空的,但我不确定这是否可行,我用它来询问这个问题。
方法
关于你的其他问题:Hibernate用于将数据库行映射到对象。如果一个线程有回复,Hibernate永远不会通过返回一个空列表来告诉你它没有任何回应。这将是一个严重的错误。如果您不想加载回复,则不要在回复列表中调用任何方法。
I have a JPA setup in such a way that if I do not use lazy load, almost the entire database will be loaded. I also use serializing directly on the models so sometimes I need to initialize the proxies.
I only want to use lazy load on the collections. The fact that some singular entities are fetched eagerly works just fine. But no matter how I try to setup the collections I never get a collection of proxies, I always get the fully loaded collection.
This is some example code:
@Entity
public class Thread implements Externalizable {
@OneToMany(mappedBy = "parentThread", fetch = FetchType.LAZY)
public List<Reply> getReplies() {
return replies;
}
So the problem here is that when I check the debugger, the persistantBag-list of replies are always filled with info and are the actual Reply objects instead of empty proxies which I want.
I use entityManager.find(Thread.class, "ID") when I want the thread and all collections like these are always fully loaded no matter if I have fetch = FetchType.LAZY or not.
As far as I understand, setting fetchtype lazy should work as I want it to. Also I would like the entities to be loaded when using the thread.getReplies() so that I can serialize and send them to the client. I do not know if getReplies will work with proxied entities since I never got any collection to be lazily loaded.
On a side not I use Intellij and used it to setup JPA with Hibernate.I have also asked a similar question where I want the collection to be completely empty but I am not sure if that is possible and I am therefor asking this question instead.
JPA Hibernate want lazy load to return empty collection
I have not yet seen a good answer on this seemingly basic question on stackoverflow. Please only reply with a link if the question is really answered in a pedagogical way since I am new to JPA/Hibernate and really databases as well.
Thank you very much in advance!
解决方案 A lazy collection doesn't contain proxies. The collection itself is a lazy-loaded collection. This means that when you get a thread from the database, its list of replies won't be initialized.
Calling getReplies()
will simply return this not-initialized list. Only when calling a method on the list itself (like size()
or iterator()
) will Hibernate initialize the list by executing a SQL query loading the state of all the replies of the thread.
Using a debugger to check what the collection contains is not a good idea, because the debugger usually calls methods on the list behind your back, which causes the list to initialize itself. You can check if a collection is initialized using the Hibernate.isInitialized(thread.getReplies())
method.
Regarding your other question: Hibernate is used to map database rows to objects. If a thread has replies, Hibernate will never tell you that it doesn't have any by returning you an empty list. That would be a serious bug. If you don't want to load the replies, simply don't call any method on the list of replies.
这篇关于JPA Hibernate集合不会被延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!