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!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…