Two big mistakes you might make with JPA and how to avoid them

แชร์
ฝัง
  • เผยแพร่เมื่อ 10 พ.ย. 2024

ความคิดเห็น • 27

  • @shawnd9492
    @shawnd9492 2 ปีที่แล้ว +1

    Thank you sir, this was a great tip. Please keep them coming. I am very excited for your new book as well.

    • @laurspilca
      @laurspilca  2 ปีที่แล้ว +1

      Thanks, Shawn!

  • @dd1.d
    @dd1.d 2 ปีที่แล้ว +1

    thank you, it was very useful and now I realized what a mess my project is

  • @dominik5451
    @dominik5451 2 ปีที่แล้ว +1

    Excellent stream, I like your explanation and examples. I've been watching your jpa/hibernate playlist also - It's good but more certification and theory-oriented and I like it, but this is the good stuff.
    I would want to have only two more wishes.
    One is to have more time to watch your videos and code along. And the second thing is you to produce even more practical streams like this about java/jpa/rdbms and real-world app problems and best practices with spring, jpa etc.

  • @RajeevanKuganathan
    @RajeevanKuganathan 2 ปีที่แล้ว +1

    I encountered the same issues during my development and I also came up with the same solution. Thanks for suggesting VisualVM. How to handle 'n+1' Query issue when there is a cyclic reference. For example, In your Classroom entity has a reference to the Student such as ClassLeader.

    • @laurspilca
      @laurspilca  2 ปีที่แล้ว

      Hello. Do you mean cyclic or transient reference? It's transient only if class leader is also a student record. If you keep it in a separate table that it's transient.

    • @RajeevanKuganathan
      @RajeevanKuganathan 2 ปีที่แล้ว +1

      @@laurspilca I was talking about if the Class leader is also a Student (Student table)

    • @laurspilca
      @laurspilca  2 ปีที่แล้ว

      @@RajeevanKuganathan Ok, but in that case, you can apply @GraphEntity on that field. I don't see why that shouln't work.

  • @laurspilca
    @laurspilca  2 ปีที่แล้ว

    Code on GitHub: github.com/lspil/youtubechannel/tree/master/two_big_problems_with_JPA

  • @shankaraec
    @shankaraec 2 ปีที่แล้ว

    You r just amazing. All your video are excellent..super excellent

  • @subba18
    @subba18 2 ปีที่แล้ว +1

    Does the unwanted update only occurs with @Transactional annotation? If we remove the annotation, This is not a problem?

    • @laurspilca
      @laurspilca  2 ปีที่แล้ว +1

      Yes. If you remove the @Transactional annotation no update can occur anymore since you don't have a transaction.

  • @jaysizmir9432
    @jaysizmir9432 2 ปีที่แล้ว +1

    Second problem starts at 39:15

  • @rahhi9792
    @rahhi9792 2 ปีที่แล้ว +1

    Thank you! if we detach the entity from the context before any changes then will it update the changes into DB.

    • @laurspilca
      @laurspilca  2 ปีที่แล้ว +2

      Hi Rahhi. No. If the entity is no longer in the context, then the changes on it will not be mirrored. However, I don't think that's what you'd like to do in most cases in a prod app. If you don't want changes, you rather never make them at all.

    • @rahhi9792
      @rahhi9792 2 ปีที่แล้ว +1

      @@laurspilca Thank you

  • @acronis536
    @acronis536 2 ปีที่แล้ว

    Extending your repository with QueryByExampleExecutor would allow you to use query by Example methods. I think that's what that question regarding "example" was all about.

    • @laurspilca
      @laurspilca  2 ปีที่แล้ว +3

      Got it. Thanks. That'll be another video I guess :)

  • @alexandralakatos3651
    @alexandralakatos3651 2 ปีที่แล้ว +1

    Hi Laur! Awesome video, really helpful. I am curious why are you always creating the tables manually in the database instead of letting the ORM do that?

    • @laurspilca
      @laurspilca  2 ปีที่แล้ว

      Hi Alexandra. Thanks for the question. In a real-world app, we never allow Hibernate (or any other JPA implementation) to create the tables, so I don't want through my tutorials to make anyone believe this is a good practice. Thus, I create the tables manually so stay as close as possible to how a real-world app works. Of course, in a real-world app we would also version our migration scripts using either Flyway or Liquibase. But never through the ORM framework.

    • @alexandralakatos3651
      @alexandralakatos3651 2 ปีที่แล้ว

      @@laurspilca Thanks a lot!

  • @rinorahmeti462
    @rinorahmeti462 2 ปีที่แล้ว +1

    Is there a way to exclude some child properties on the entity graph?
    For example the classroom has 2 child elements; students and another entity e.g professors (both of them are one to many from classroom).
    Now, on my 1st endpoint I want to return all the classrooms and ONLY the students linked to them (so I want to left join only the students table and exclude the join with the other child (or other children elements if there are more relations), whereas on my 2nd enpoint I want to return the classrooms and all their child elements (in this case I want to left join the students table and the professors table).
    I could have an entity with 10 child elements (mostly many to one) and I don't want to expose all the child elements on every endpoint and join 10 tables everytime I want to fetch this entity; I would like to return specific child properties on specific endpoints.
    I hope my question is clear, thanks a lot!

    • @laurspilca
      @laurspilca  2 ปีที่แล้ว +1

      Hi. I guess you can make the field fetch LAZY?

    • @laurspilca
      @laurspilca  2 ปีที่แล้ว +1

      I mean, make the fields lazy and fetch them only when you need, only what you need on the query.

    • @rinorahmeti462
      @rinorahmeti462 2 ปีที่แล้ว +1

      @@laurspilca thanks I have tried that but now for example if I want to return in response the parent with for example 5 child elements (5 lists with tens of elements for example), I won't have one query with 5 joins but more queries.. just wanted to know whether it is possible to make any preconfiguration in cases where I know exactly what I will need to return in response.. because even if I write a @Query above the method, it still doesn't seem to work the way I want to - it still fetches the other elements

    • @laurspilca
      @laurspilca  2 ปีที่แล้ว

      @@rinorahmeti462 Hm. Strange. I'm not sure about your case. Maybe I don't understand it. Usually you should be able to use a JOIN fetch on the query for exactly what you need while leaving the others lazy.

    • @rinorahmeti462
      @rinorahmeti462 2 ปีที่แล้ว +1

      @@laurspilca I had one ManyToMany relationship which was causing the issue perhaps.. if I am able to reproduce that case I will let you know because looks to be an interesting issue. Thanks!