JPA Hibernate collections not lazily loaded

时间秒杀一切 提交于 2019-11-29 12:55:22

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!