The best way to map a @OneToOne relationship with JPA and Hibernate

Поділитися
Вставка
  • Опубліковано 2 жов 2022
  • In this video, we are going to learn the best way to map a @OneToOne association with JPA and Hibernate.
    While there are many ways you can map a one-to-one relationship with Hibernate, I’m going to demonstrate which mapping is the most efficient one from a database perspective.
  • Наука та технологія

КОМЕНТАРІ • 28

  • @jalilhasanli5577
    @jalilhasanli5577 Рік тому +1

    Today I have heard about you from my colleague, and I fell in love with your articles and now I am very glad to see you here. Thanks in advance))

  • @piotrwojcik1400
    @piotrwojcik1400 Рік тому +3

    Awesome content as always, keep up the good work!

  • @fabricio.entringer
    @fabricio.entringer Рік тому +1

    Hello @Vlad, this video is perfect! It's a complex topic but your explanation is so good. Thanks a lot.

    • @vladmihalcea
      @vladmihalcea  Рік тому

      I'm glad you liked it and stay tuned for more

  • @MetalIsTheLaw1993
    @MetalIsTheLaw1993 Рік тому +1

    Excelent video, it was so helpful. Thanks!

  • @Jp-bb4cv
    @Jp-bb4cv 6 днів тому

    very good lecture, many tks!

    • @vladmihalcea
      @vladmihalcea  6 днів тому +1

      You're welcome. If you liked this snippet, you are going to love the video course where this snippet is coming from:
      vladmihalcea.com/courses/high-performance-java-persistence/

  • @melancholy-engineering
    @melancholy-engineering Рік тому +1

    Thanks for the great video, as always. Could you please share what visualization tool you are using at 0:45?

    • @vladmihalcea
      @vladmihalcea  Рік тому +1

      The class diagrams were generated with IntelliJ IDEA.
      The DB table diagrams with MySQL Browser.
      The rest of the diagrams were generated with yEd.

  • @mohansharma1989
    @mohansharma1989 Рік тому +1

    Perfect. A video series would definitely be a great add-on for all the hibernate blogs you've written

    • @vladmihalcea
      @vladmihalcea  Рік тому

      This video is an episode of the 11-hour video course, High-Performance Java Persistence.
      So, if you enjoyed this video episode, you are going to love this video course:
      vladmihalcea.com/courses/high-performance-java-persistence/

  • @wojtek120pl
    @wojtek120pl Рік тому +1

    Great video! Now, I'm thinking about whole course :) please, tell me why children entity should be owning side of relation?

    • @vladmihalcea
      @vladmihalcea  Рік тому +1

      I'm glad you liked it. In fact, you are going to love the video course even more.
      As for your question, the entity that maps the FK column is the one that should own the relation because FK column values are supposed to be managed via their associated table record.

  • @kshitizkumar6258
    @kshitizkumar6258 Рік тому

    Video sound is low.
    Btw good explanation.

    • @vladmihalcea
      @vladmihalcea  Рік тому

      On my latest videos, I increased the volume.

  • @fahmydiab
    @fahmydiab Рік тому +1

    why do i need to have a one to one relationship? I mean i can wrap the details or include it in the post table

    • @vladmihalcea
      @vladmihalcea  Рік тому +3

      That's a very good question that I frequently get during my training. The @OneToOne relationship basically maps the one-to-one table relationship that was designed 40 years ago and which serves plenty of use cases, like the following ones:
      - if you have a JSON column that's large, you might not want to fit it on the same page as the original record as that could create page splits, increase the volume of data that gets synced to the disk when the pages get dirty
      - if you have columns that have different change frequencies, you can split them by write access patterns so that you could have the columns that frequently change in a separate table whose pages get synced often, but they leave the other table pages to reside in the Buffer Pool
      - to implement the JOINED table inheritance strategy
      - to split a large table into multiple ones to increase write scalability as UPDATEs and DELETEs take locks at record level in relational databases, and once you change a single column, the entire record gets locked until you commit or roll back. But by splitting the original table into several tables, you can increase the write throughput.
      - to reduce the optimistic locking conflicts as you have multiple version columns in multiple tables. If you only have a single table, then any column change will cause the other concurrent transactions that also want to change the same record to abort after the first transaction has bumped the version up.

  • @TheSemenFarada
    @TheSemenFarada 11 місяців тому

    Why not make a Post as a relation owner?

  • @aleemkhowaja2274
    @aleemkhowaja2274 Рік тому

    How can we achieve this feature in spring data jpa?

    • @vladmihalcea
      @vladmihalcea  Рік тому

      Which feature are you talking about?

    • @aleemkhowaja2274
      @aleemkhowaja2274 Рік тому

      @@vladmihalcea I mean one to one relationship without n+1 problem which you described in video

    • @vladmihalcea
      @vladmihalcea  Рік тому

      @@aleemkhowaja2274 The solution for that can be done with a Maven or Gradle plugin that it's independent of Spring. So, it works I the same way with Spring as it does with Java EE.

    • @aleemkhowaja2274
      @aleemkhowaja2274 Рік тому

      @@vladmihalcea ahh ok yaah makes sense to me thanks bro btw great explanation in video and thank you so much for response cheers 🥂 🙂

    • @vladmihalcea
      @vladmihalcea  Рік тому

      @@aleemkhowaja2274 You're welcome. If you liked this video, there's a 40% discount on the video course:
      vladmihalcea.teachable.com/p/high-performance-java-persistence-mach-3-online/?coupon_code=BLACKFRIDAY22
      But hurry up! It expires in 2 days.