Difference between FetchType LAZY and EAGER in Java Persistence API?

后端 未结 15 1301
鱼传尺愫
鱼传尺愫 2020-11-22 08:08

I am a newbie to Java Persistence API and Hibernate.

What is the difference between FetchType.LAZY and FetchType.EAGER in Java Persistence API?

15条回答
  •  情深已故
    2020-11-22 08:46

    Both FetchType.LAZY and FetchType.EAGER are used to define the default fetch plan.

    Unfortunately, you can only override the default fetch plan for LAZY fetching. EAGER fetching is less flexible and can lead to many performance issues.

    My advice is to restrain the urge of making your associations EAGER because fetching is a query-time responsibility. So all your queries should use the fetch directive to only retrieve what's necessary for the current business case.

提交回复
热议问题