Java Spring Boot - JPA - Hibernate - H2 - One To One Bidirectional Relationship - @OneToOne

Поділитися
Вставка
  • Опубліковано 9 чер 2020
  • In this video we will implement one to one bidirectional relationship using Java, Spring Boot, JPA, Hibernate and H2 in memory database. We will start with generating project and adding all of the necessary dependencies. Later on we will create entities and then we will create a one to one relationship between them. We will persist example data in database and after this we will retrieve it using CRUD repositories to test out if our mapping works correctly. We will track everything using H2 console. Everything with in-depth explanation! Keep playing with code!
    Follow //CodefForgeYT on:
    FB: / codeforgeyt
    Twitter: / codeforgeyt
    GitHub: github.com/codeforgeyt
    Instagram: / codeforgeyt

КОМЕНТАРІ • 26

  • @kotinakarwak7857
    @kotinakarwak7857 3 роки тому +1

    Thanks for the tutorials.
    I am working through this one and you mentioned that recursion errors would be eliminated by code (toStrings being specific and remove the 'pointed' to class). When I run a restApi to see owners and on cars, think this is still an issue which only becomes apparent when you try and access the data in restApi mode. Please check if this is the case.
    My hunt for a workaround/setup that will eliminate the recursion issues pointed me to use the following annotations in the classes/Entities.
    @JsonManagedReference (on themapped side)
    @JsonBackReference (on the @joincolumn side)
    Think an explanation might call for another video ;)

    • @CodeForgeYT
      @CodeForgeYT  3 роки тому +1

      Hey! Thanks for feedback. Yes, in this example if you will try to access data thorugh the API you will encounter a problem with recursion.
      This problem is the result of using jackson. Jakcosn will inifinitely loop through this bidirectional reference when serializing to the JSON format.
      To solve this you have two options.
      First one is to use annotations you have mentioned. Thanks to that you can use your entities as DTOs.
      Second approach is about creating extra classes that are equivalent to our entities, but they don't have references to each other. Those are so called DTOs which stands for data transfer objetcs.
      I am showing the second approach in one of the videos where we are creating one to many web service. If you are curious about second approach I encourage you to check it out!
      I will think about creating video with those annotations, thanks! Keep coding!

  • @AjaySingh-xd4nz
    @AjaySingh-xd4nz 3 роки тому +1

    Thanks much! Nicely explained.

  • @TroenderTass
    @TroenderTass 2 роки тому +1

    You should emphasis the importance of not including the mapped entities in the toString method. That causes a recursive overflow where each entity keeps printing eachother until a memory overflow.

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

      Thanks for feedback. It is more likely Java related topic. Recursive calls of toString or NPR exceptions when calling it are very common issues in Java world. It is not related with the topic of the video.

  • @rakeshkodam345
    @rakeshkodam345 2 роки тому +1

    Great Content 👍

  • @dungphamtien4027
    @dungphamtien4027 2 роки тому +1

    very wonderful, sir

  • @niled.r.1639
    @niled.r.1639 3 роки тому +1

    Hi! thank you very much for the videos. I was following the instructions and the application runs but when I try to open the H2 console I receive a Whitelabel error Page..( I tried to put different examples in the same project but on different packages, tried to fix it by separating them in new different projects but did not help) . I would appreciate some recommendations/hints.
    Thanks again.

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

      Hi!
      Thanks for the feedback! I am glad you like the video.
      Regarding your problem, it is hard to say what is wrong. Try to watch my video about setting up H2 database in Spring Boot project. You can find it in Java Spring Boot Tutorials and Java Spring Boot JPA playlists.
      I hope you will find your solution over there. Keep coding!

    • @niled.r.1639
      @niled.r.1639 3 роки тому +1

      @@CodeForgeYT yes! I will keep trying and learning

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

      @@niled.r.1639 Great!

  • @ChavyThai
    @ChavyThai 7 місяців тому

    epic

  • @martonkovacs3673
    @martonkovacs3673 3 роки тому +1

    I tried to implement this, but when I type Table(name = ???) then it says cannot resolve table, and underlines it with red. Any ideas why?

    • @CodeForgeYT
      @CodeForgeYT  3 роки тому +1

      No idea, post code snippet on stackoverflow or try to google it. Happy Coding!

  • @shahidurrahman5848
    @shahidurrahman5848 3 роки тому +1

    very nice explanation. Please make the same example using webapplication run in browser. Thank you.

    • @CodeForgeYT
      @CodeForgeYT  3 роки тому +1

      Thanks! I am working on one to many backend API right now. After finishing it I will think about client for it.

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

      @@CodeForgeYT Please consider Angular as a client for your one to many backend API. Thank you.

    • @CodeForgeYT
      @CodeForgeYT  3 роки тому +1

      @@shahidurrahman5848 Sure, I will. Thanks for feedback!

  • @preyas2198
    @preyas2198 3 роки тому +1

    Nice Explanation..
    I've one doubt that why "id" start from 2 in "OWNER" table.. What to do to start it from 1 ???
    Please give me suggestion if anyone have

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

      Change the strategy of ids generation, but it doesn't matter from which number it starts

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

      Ok

  • @karelinyoutube7073
    @karelinyoutube7073 3 роки тому +1

    Why don't you use @Autowired?

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

      Try to use it and let me know if it worked! :)