Obviously, criteria queries are more difficult to read if they get more complex—a good reason to prefer them for dynamic and programmatic query generation, but to use externalized HQL and JPA QL for predefined queries.
A proxy is initialized if you call any method that is not the identifier getter
method, a collection is initialized if you start iterating through its elements or if
you call any of the collection-management operations, such as size() and contains().
Hibernate provides an additional setting that is mostly useful for large collections; they can be mapped as extra lazy.
@OneToMany
@org.hibernate.annotations.LazyCollection(
org.hibernate.annotations.LazyCollectionOption.EXTRA
)
private Set<Bid> bids = new HashSet<Bid>();
The collection is no longer
initialized if you call size(), contains(), or isEmpty()—the database is queried
to retrieve the necessary information. If it’s a Map or a List, the operations containsKey()
and get() also query the database directly.
本文深入探讨了Hibernate中的懒加载机制及其在集合初始化时的应用,解释了proxy和lazy collection的概念,并通过示例代码说明了如何合理使用这些特性以优化性能。

424

被折叠的 条评论
为什么被折叠?



