5 ways to initialize lazy associations and when to use them

Поділитися
Вставка
  • Опубліковано 28 тра 2024
  • Lazy loading of associated entities is a well-established best practice in JPA. Its main goal is to retrieve only the requested entities from the database and to load the related entities only if needed. That is a great approach if you only need the entities you selected in your query. But it creates additional work and can be the cause of performance problems if you also need some of the associated entities.
    In this video, I show 5 different ways to trigger the initialization of lazy associations and their specific advantages and disadvantages.
    Links mentioned in the video:
    JPA 2.1 Entity Graph - Part 1: Named entity graphs: goo.gl/K6sFHv
    JPA 2.1 Entity Graph - Part 2: Define lazy/eager loading at runtime: goo.gl/sSFbke
    Read the accompanying post: www.thoughts-on-java.org/5-wa...
    Join the free Thoughts on Java Library: goo.gl/OSG8rH
    Like my channel? Subscribe!
    ➜ bit.ly/2cUsid8
    Want to connect with me?
    Blog: www.thoughts-on-java.org/
    Twitter: / thjanssen123
    Facebook: / thoughtsonjava
  • Наука та технологія

КОМЕНТАРІ • 15

  • @peterraynor
    @peterraynor 5 років тому

    Thorben thank you very much, this video helps me to fix a problem with lazy init in hibernate using querydsl.

  • @TheodoreRavindranath
    @TheodoreRavindranath 2 роки тому

    This content seems a bit outdated! However, this is still one of the top videos if I search "jpa lazy loading"! Doesn't JPA do joins automatically these days, unless something is marked as lazy? Or is that the default only in case of Spring Data JPA CrudRepository? Maybe Spring Data JPA automatically creates Join Fetches?

  • @TimoSurfs
    @TimoSurfs 3 роки тому

    Great video!

  • @obrunobitencourt
    @obrunobitencourt 7 років тому +1

    Great lesson!
    I have a question...
    I need access some properties from objects that can have many relationships mapped as lazy. The problem is that i must access these values by reflection, even calling the get method doesn't work, the value still null. I'm thinking in use the entity graph to "detect" the relationships that i need load.
    Does the hibernate have a resource to do that in a more generic way? Something like find the entity with all relationships mapped as lazy whithout explicitely defining one by one with "JOIN FETCH"?

    • @ThoughtsOnJava
      @ThoughtsOnJava  7 років тому +1

      Hi Bruno,
      I'm not sure if I understand your question correctly.
      If Hibernate shall fetch a lazy association, you either need to reference it in an entity graph, a JOIN FETCH clause or you need to call the getter method on the attribute that maps the association.

  • @jimmyoktora
    @jimmyoktora 5 років тому

    nice video, thanks.
    can you give me some advice whats going on with lazy at hibernate after 5.2.12?
    it said lazy init when i access my lazy data, but it works fine before that version.
    this is the simple illustration:
    before 5.2.12 -> user.getRole().getId() enough to fetch the whole role
    after 5.2.12 -> user.getRole().getId() NOT ENOUGH to fetch the role
    thanks

  • @SravanKumar-qc5hm
    @SravanKumar-qc5hm 5 років тому

    How to implement paging/batching for loading the associated entities in Hibernate/JPA?

  • @HeavenlyCitizenTamil
    @HeavenlyCitizenTamil 7 років тому +2

    Nice video mate.. Cheers :)

  • @vsg-ch
    @vsg-ch 7 років тому

    Hi! thank you for the excellent explanation! ...I have a question ...when the graph entity is actually retrieved from the database? or when exactly the entity graph is loaded into the persistence context? once the class is loaded, once the transaction starts, after calling .getEntityGraph()?... thx! :)

    • @ThoughtsOnJava
      @ThoughtsOnJava  7 років тому +2

      Hi,
      all entities of the graph are loaded when Hibernate loads the entity or performs the query to which you added the hint with the graph. That happens when you call the find() method on the EntityManager or when you execute the query with getSingleResult() or getResultList().

  • @MartinCharlesworth73
    @MartinCharlesworth73 7 років тому

    Hi Thorben, thanks for another excellent video. Just one possible improvement - could you use presenter mode or just a bigger font when showing code in the editor pls, it is to small to see on a phone screen. Cheers!

    • @ThoughtsOnJava
      @ThoughtsOnJava  7 років тому +1

      Hi Martin,
      thanks your comment. I will try to improve that in future videos. But I already recorded the ones for the next week. So, it will take a few videos until you can see a change :(

  • @vaibhavm1986
    @vaibhavm1986 5 років тому

    Hi Thorben,
    I am facing one unique situation , I have around 10 left outer joins in one of my query and number of records are 3.2 million
    If we choose any of the strategies that are based on JOIN , its taking 20 seconds for a batch size of 100 records because of high Cartesian product it seems , I am looking for a solution that can get rid of JOINS , and load associated entities without executing single query for each of them , I am basically doing pagination where I need 100 records on each page
    Does hibernate provide any solution for such use case , I have done lot of research on google , could not find a solution
    Please help
    Thanks ,
    Vaibhav Mittal

  • @AurelioCE88
    @AurelioCE88 6 років тому

    Hi , i'm using repository (CrudRepository) how i can fix N+1 issue?