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

  Рет қаралды 14,312

CodeForgeYT

CodeForgeYT

Күн бұрын

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/cod...
Instagram: / codeforgeyt

Пікірлер: 26
@kotinakarwak7857
@kotinakarwak7857 3 жыл бұрын
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 жыл бұрын
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 жыл бұрын
Thanks much! Nicely explained.
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
Glad to help!
@TroenderTass
@TroenderTass 2 жыл бұрын
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.
@dungphamtien4027
@dungphamtien4027 2 жыл бұрын
very wonderful, sir
@CodeForgeYT
@CodeForgeYT 2 жыл бұрын
Many many thanks
@rakeshkodam345
@rakeshkodam345 2 жыл бұрын
Great Content 👍
@CodeForgeYT
@CodeForgeYT 2 жыл бұрын
Thank you 🙌
@shahidurrahman5848
@shahidurrahman5848 3 жыл бұрын
very nice explanation. Please make the same example using webapplication run in browser. Thank you.
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
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 жыл бұрын
@@shahidurrahman5848 Sure, I will. Thanks for feedback!
@ChavyThai
@ChavyThai 10 ай бұрын
epic
@preyas2198
@preyas2198 3 жыл бұрын
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
@martonkovacs3673
@martonkovacs3673 3 жыл бұрын
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 жыл бұрын
No idea, post code snippet on stackoverflow or try to google it. Happy Coding!
@niled.r.1639
@niled.r.1639 3 жыл бұрын
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 жыл бұрын
@@CodeForgeYT yes! I will keep trying and learning
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
@@niled.r.1639 Great!
@karelinyoutube7073
@karelinyoutube7073 3 жыл бұрын
Why don't you use @Autowired?
@CodeForgeYT
@CodeForgeYT 3 жыл бұрын
Try to use it and let me know if it worked! :)
❌Разве такое возможно? #story
01:00
Кэри Найс
Рет қаралды 7 МЛН
ضربت اختها هي وعم تعمل عرض ازياء 🥹 #youtubeshorts #baby
00:12
Maria & Cataleya Official
Рет қаралды 3,4 МЛН
JPA / Hibernate One to One Mapping Example with Spring Boot
21:26
One To One Bidirectional Mapping in hibernate
15:02
KK JavaTutorials
Рет қаралды 10 М.
Spring Data JPA from 0-100 in 60 minutes
1:02:31
VMware Tanzu
Рет қаралды 81 М.
Spring Boot | JPA | MVC | H2 Example
15:09
Telusko
Рет қаралды 238 М.
Spring Boot Validation
11:58
Fast and Simple Development
Рет қаралды 20 М.
❌Разве такое возможно? #story
01:00
Кэри Найс
Рет қаралды 7 МЛН