热门面试题与答案和在线测试
面向面试准备、在线测试、教程与实战练习的学习平台

通过聚焦学习路径、模拟测试和面试实战内容持续提升技能。

WithoutBook 将分主题面试题、在线练习测试、教程和对比指南整合到一个响应式学习空间中。

面试准备

模拟考试

设为首页

收藏此页面

订阅邮箱地址

Without Book 提问

问题. Difference between Eager loading and Lazy Loading in Hibernate.

- Please explain with difference between Eager loading and lazy loading and in which situation which should be used.

发布时间 Jul 23, 2014 发布者 Ruby Singh
回答. Sometimes you have two entities and there\'s a relationship between them. For example, you might have an entity called Organization and another entity called Employee.

The Organization entity might have some basic properties such as id, name, address, etc. as well as a property called students:

public class Organization {
private String id;
private String name;
private String address;
private List<employee> employees;

// setters and getters
}
Now when you load a Organization from the database, JPA loads its id, name, and address fields for you. But you have two options for employees: to load it together with the rest of the fields (i.e. eagerly) or to load it on-demand (i.e. lazily) when you call the Organization\'s getEmployees() method.

When a organization has many employees it is not efficient to load all of its employees with it when they are not needed. So in suchlike cases, you can declare that you want employees to be loaded when they are actually needed. This is called lazy loading.

Any time you see a @OneToOne, @OneToMany or @ManyToMany annotations, youve got a relationship. Whats important to note is that the fetch type should be specified within those annotations, if you dont specify one then it defaults to FetchType.LAZY.
发布时间 Jul 28, 2014 发布者 Arindam Ghosh

输入你的回答

姓名
邮箱地址
回答
安全验证
继续前请计算: 4 + 3 = ?
版权所有 © 2026,WithoutBook。