Difference between FetchType LAZY and EAGER in Java Persistence API?

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

    I may consider performance and memory utilization. One big difference is that EAGER fetch strategy allows to use fetched data object without session. Why?
    All data is fetched when eager marked data in the object when session is connected. However, in case of lazy loading strategy, lazy loading marked object does not retrieve data if session is disconnected (after session.close() statement). All that can be made by hibernate proxy. Eager strategy lets data to be still available after closing session.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 08:44

    I want to add this note to what "Kyung Hwan Min" said above.

    Suppose you are using Spring Rest with this simple architect:

    Controller <-> Service <-> Repository

    And you want to return some data to the front-end, if you are using FetchType.LAZY, you will get an exception after you return data to the controller method since the session is closed in the Service so the JSON Mapper Object can't get the data.

    There is three common options to solve this problem, depends on the design, performance and the developer:

    1. The easiest one is to use FetchType.EAGER, So that the session will still alive at the controller method.
    2. Anti-patterns solutions, to make the session live until the execution ends, it is make a huge performance issue in the system.
    3. The best practice is to use FetchType.LAZY with converter method to transfer data from Entity to another data object DTO and send it to controller, so there is no exception if the session closed.
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 08:46

    public enum FetchType extends java.lang.Enum Defines strategies for fetching data from the database. The EAGER strategy is a requirement on the persistence provider runtime that data must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime that data should be fetched lazily when it is first accessed. The implementation is permitted to eagerly fetch data for which the LAZY strategy hint has been specified. Example: @Basic(fetch=LAZY) protected String getName() { return name; }

    Source

    0 讨论(0)
  • 2020-11-22 08:48

    From the Javadoc:

    The EAGER strategy is a requirement on the persistence provider runtime that data must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime that data should be fetched lazily when it is first accessed.

    E.g., eager is more proactive than lazy. Lazy only happens on first use (if the provider takes the hint), whereas with eager things (may) get pre-fetched.

    0 讨论(0)
提交回复
热议问题