Difference between FetchType LAZY and EAGER in Java Persistence API?

后端 未结 15 1303
鱼传尺愫
鱼传尺愫 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:44

    The Lazy Fetch type is by default selected by Hibernate unless you explicitly mark Eager Fetch type. To be more accurate and concise, difference can be stated as below.

    FetchType.LAZY = This does not load the relationships unless you invoke it via the getter method.

    FetchType.EAGER = This loads all the relationships.

    Pros and Cons of these two fetch types.

    Lazy initialization improves performance by avoiding unnecessary computation and reduce memory requirements.

    Eager initialization takes more memory consumption and processing speed is slow.

    Having said that, depends on the situation either one of these initialization can be used.

提交回复
热议问题