Spring Boot | Spring Data JPA Many To Many Bidirectional Relationship Example | JavaTechie

  Рет қаралды 67,347

Java Techie

Java Techie

Жыл бұрын

In this tutorial, you'll learn how to map a many-to-many bidirectional relationship using Spring Data JPA and MySQL in a Spring Boot application. Many-to-many relationships are one of the most commonly used association mappings. They require an additional database table to hold the primary keys of both relationship tables.
Spring boot microservice Live course Just started
Hurry-up & Register today itself!
COURSE LINK : javatechie5246.ongraphy.com/
PROMO CODE : Java40
GitHub:
github.com/Java-Techie-jt/JPA...
Blogs:
/ javatechie
Facebook:
/ javatechie
guys if you like this video please do subscribe now and press the bell icon to not miss any update from Java Techie
Disclaimer/Policy:
--------------------------------
Note : All uploaded content in this channel is mine and its not copied from any community ,
you are free to use source code from above mentioned GitHub account

Пікірлер: 136
@pranaypaul6361
@pranaypaul6361 10 күн бұрын
Excellent video in terms of keeping it simple throughout focussing on the core basic of many to many!! One of the best out there.Thank you.
@user-bt6pi5wk1x
@user-bt6pi5wk1x Жыл бұрын
I've been stuck for hours not being able to create the bidirectional relationship, it was always caught from only the one end and I was going mad as to why... and it turned out it is the thing you mention at 6:28, because I was using @Data in both places. Thank you so much man, appreciate the tutorial!
@rslakshmilifestyle
@rslakshmilifestyle 8 ай бұрын
Thank you so much ur comment helped me to fix my issue. 😊
@user-bt6pi5wk1x
@user-bt6pi5wk1x 7 ай бұрын
@@rslakshmilifestyle glad it helped 🙏
@georgikyshenko4380
@georgikyshenko4380 11 ай бұрын
Exactly what i needed. Great job. Thanks !
@g.ariywt
@g.ariywt 4 ай бұрын
Thank you for helping me understand how to use JPA and Many-to-Many relationships clearly..
@alexgutjahr
@alexgutjahr Жыл бұрын
Great tutorial! Many-to-many relationships are so common in most applications. Yet I often see people struggle with setting up the relationships in the entities - they assume there has to be ONE right way of doing it, not realizing that EACH entity can own the relationship.
@Javatechie
@Javatechie Жыл бұрын
Agree with you
@florjandyshniku6464
@florjandyshniku6464 Жыл бұрын
@@Javatechie Great video! But if you add another student with same courses will duplicate courses. Can you solve this problem?
@musanzawanga2083
@musanzawanga2083 Жыл бұрын
This tutorial is a successful in explaining explain ManyToMany Relationships in Java! ✔️You made it easy to follow! ✔️Thanks alot Java Techie 💙
@roseodhiambo703
@roseodhiambo703 8 ай бұрын
I agree with you 💯.
@lemrabottabdelaziz4357
@lemrabottabdelaziz4357 Жыл бұрын
Amazing as always !
@supun_sandaruwan
@supun_sandaruwan 5 ай бұрын
Great Tutorial, Thank you sir♥
@sandeeps1093
@sandeeps1093 4 ай бұрын
Its really a good aproach , Thanks Techie
@rekhapandit2797
@rekhapandit2797 Жыл бұрын
Thank u so much....its very helpful vedio....to understand many to many relationships.....please bring more vedios like as....also request bring the vedio on online shopping in springboot with react js...
@alparslancavdar2292
@alparslancavdar2292 Жыл бұрын
Excellence work.thx a lot
@fsdinterviewguide
@fsdinterviewguide Жыл бұрын
Useful video, thank you
@mutturajdeval4841
@mutturajdeval4841 Жыл бұрын
Great explanation 🥰🥰
@alelasgrutas
@alelasgrutas Жыл бұрын
thanks ! excellent
@sharadkumar6658
@sharadkumar6658 10 ай бұрын
Nice sir It works.
@dhanshyamkamath8493
@dhanshyamkamath8493 Жыл бұрын
Good video.
@ChandlerBing11
@ChandlerBing11 Жыл бұрын
Such a quite nice content as always. Thank you, Java Techie
@shubhamtopare9720
@shubhamtopare9720 Жыл бұрын
Thanks for the informative video, one request - if you give quick summary at the end of the video i.e. what we did from start to end with the code, it will be very easy to remember the things.
@Javatechie
@Javatechie Жыл бұрын
Good suggestion Subham . going forward will do same
@shubhamtopare9720
@shubhamtopare9720 Жыл бұрын
@@Javatechie Thank you.
@hkkabir2024
@hkkabir2024 Жыл бұрын
good presentation but you should give all type of operations from both sides
@PradeepKumar-sx6ms
@PradeepKumar-sx6ms Жыл бұрын
In many to many directional, there are challenges in add & delete please discuss on that
@yacinedrissi8811
@yacinedrissi8811 Жыл бұрын
Hi thank you for your effort please could you do a crud operation with many to many thank you
@thimothipalipaka852
@thimothipalipaka852 Жыл бұрын
Hii in my project I am facing one problem, user having usercart that usercart contains products , if i add product it mapped to cart of a particular user , if add another product for same user cart it overrides the previous one how to overcome this situation .
@roseodhiambo703
@roseodhiambo703 8 ай бұрын
Thank you 🙌🙌sir.Then I have one question am confuse.the annotations @JsonManagedReference and this called MapStruct/Mapper .They do the same thing or ? am confused please help.Thank you
@ustemdogge4976
@ustemdogge4976 Жыл бұрын
Hello, this tutorial is really good, i like it so much. But i have one question, how can i include some additional field in student-course table, for example to track the head of the group of students? Is there the way i can do this only with these annotations or i have to create entity for this?
@Javatechie
@Javatechie Жыл бұрын
No it's not possible to achieve using annotation you need to create your own entity to maintain this
@bluex217
@bluex217 2 ай бұрын
Thank you! I have question for 33:46: basically I'm confused that you used FetchType LAZY but yet findById method is still populating the list of courses eagerly. I have the same behavior, but I question why
@Manoj-zx6ye
@Manoj-zx6ye Жыл бұрын
Could make a video on jersy rest integration with swagger ui with spring boot sir.
@lovetyagi5266
@lovetyagi5266 Жыл бұрын
when u fire find all couse student not include but we have bidrectional mapping ? so whats is the point to use @JsonBackRefrence Any Other Solution
@sumithulsurkar2436
@sumithulsurkar2436 Жыл бұрын
Thanks for sharing the valuable knowledge, but I request you to please use mice as audio of your videos is very less. Also it will be so great help if you explain few of the concepts. I mean, why we are doing this way like.
@Javatechie
@Javatechie Жыл бұрын
Hi Sumit i didn't get you. Is my voice is very loud 📢 or less in video?
@shubhamkale5003
@shubhamkale5003 6 ай бұрын
How can I add same course for 2 different students?
@SandeepKumar-se2fd
@SandeepKumar-se2fd Жыл бұрын
Hey Basanta, Great Explanation. I have one doubt. From student api we are able to fetch both student and course data. Can we fetch course and enrolled student data from course api i.e. search/{price}??
@Javatechie
@Javatechie Жыл бұрын
Hi Sandeep yes we can do that
@SandeepKumar-se2fd
@SandeepKumar-se2fd Жыл бұрын
@@Javatechie Can you please guide, how to do?
@rakshitmandilwar2817
@rakshitmandilwar2817 Жыл бұрын
Java techie please help me I am getting error 415 in postman while inserting the payload.....it says the message type is not supported I tried using header as content type application json yet it doesn't work
@Javatechie
@Javatechie Жыл бұрын
Are you trying with http post method from postman
@rakshitmandilwar2817
@rakshitmandilwar2817 Жыл бұрын
@@Javatechie yes sir I am using http post method on postman for sending the data, I tried various things like using Json ignore , adding application/json in content type yet it gives error 415 and in console it gives deserialization error
@samantr
@samantr Жыл бұрын
any enterprise application will have atleast 15-20 entities and relationships between them. In such cases what is best design - defining the relationships in entity classes or other ways ?
@sdscode
@sdscode 2 ай бұрын
Yes
@medyazidhamlaoui2703
@medyazidhamlaoui2703 6 ай бұрын
thank you, I have a question, how can I add the course and assign the students of this course
@biswajeetsahoochintu2329
@biswajeetsahoochintu2329 Жыл бұрын
🙏🙏🙏
@ANANDKUMAR-0807
@ANANDKUMAR-0807 7 ай бұрын
Hi Basant, I have one small Doubt. In this Example fetch type is Lazy but while fetching student details, Course Details is getting Populated. I am getting Confused. Can you Please Clarify this. Thanks
@vijik2988
@vijik2988 Жыл бұрын
Can u please do update and delete operation also
@kotikobillionaire281
@kotikobillionaire281 Жыл бұрын
I have picked up the final version from repository, and run the code as is - without any modifications. It is not working. I am getting this error when trying to add Student: Failed to evaluate Jackson deserialization for type [[simple type, class com.javatechie.entity.Student]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'defaultReference': back reference type (`java.util.Set`) not compatible with managed type (com.javatechie.entity.Student). What could be the issue?
@user-ui9cq6rc1e
@user-ui9cq6rc1e 9 ай бұрын
Its bcz of using @JsonManagedReference and @JsonBackReference.... Did u get the solution?
@vijik2988
@vijik2988 Жыл бұрын
Please do all mapping CRUD operation since I am facing issues with update and delete
@lovetyagi5266
@lovetyagi5266 Жыл бұрын
please make video on how to create exokicit join(custom join query )with native and jpql query with and withut projection PLEASE JAVA TECHIE
@Javatechie
@Javatechie Жыл бұрын
Already uploaded please check kzbin.info/www/bejne/bqLLknd4eK-bbKs
@Javatechie
@Javatechie Жыл бұрын
Already uploaded please check kzbin.info/www/bejne/bqLLknd4eK-bbKs
@lightd1404
@lightd1404 9 ай бұрын
I got issues like this: when i insert new value in the table student_course_table its not update .It is just update data on table course_tbl and student_tbl. Can u help me =(((
@YtSeshomaru
@YtSeshomaru 7 ай бұрын
Maybe I'm wrong, but it seems to be bad. There's no many to many relationship if CRUD is controlled by Student entity, because there's not going to occur the same course for another student. If you delete a student, then all Courses that this Student owns will be deleted from Course entity too
@johnsnow1757
@johnsnow1757 Жыл бұрын
can u tell me about put mapping
@lovetyagi5266
@lovetyagi5266 Жыл бұрын
@JSONBACKREFEENCE create problem because at a time of serialization jsonback refrence data not came in api
@Javatechie
@Javatechie Жыл бұрын
It shouldn't give any issue can you please share error
@kshitijbansal3672
@kshitijbansal3672 Жыл бұрын
you are right, we cannot use @JSONBACKREFEENCE with collections, need to use @JsonIdentityInfo
@Nikhil91119
@Nikhil91119 5 ай бұрын
@@kshitijbansal3672 thanks bro
@marianojm5675
@marianojm5675 Жыл бұрын
Hello, thanks for the video, but when I do that it gives me a LAZY error, I switch to EAGER and it gives me a serialization error. Why does that happen? Thank you
@Javatechie
@Javatechie Жыл бұрын
I have shared GitHub source code in video description please compare with your code and fix
@bunny96
@bunny96 Жыл бұрын
@@Javatechie , yes, you used spring.jpa.open-in-view (by default) and because of that, you didn't get a LazyInitializationException. This is an anti-pattern, btw
@pranabkumarnath3818
@pranabkumarnath3818 Жыл бұрын
I have downloaded your project from github and pasted your payloads but I am getting unsupported media type error in postman . I tried writing manually also, bt same error i got. I am using sts ide, is there any problem with that?
@pranabkumarnath3818
@pranabkumarnath3818 Жыл бұрын
I think i should learn more about serialisation and deserialisation 😂😂😂. I used @JsonIgnore and it worked.
@beytullahyayla7401
@beytullahyayla7401 Жыл бұрын
@@pranabkumarnath3818 Heyo, where did u use JsonIgnore
@Iamit99yadav
@Iamit99yadav Жыл бұрын
Can u create same crud operation with Hibernate
@Javatechie
@Javatechie Жыл бұрын
We can do it easily. Instead of use EntityManager just use the hibernate session factory that's it .
@RajYadav-fy4lm
@RajYadav-fy4lm Жыл бұрын
can we still enroll to your ongoing course or it would be too late?
@Javatechie
@Javatechie Жыл бұрын
Hi Raj you can enroll in the course now. Theory classes are only completed and you can also see the recording session of completed classes
@muralikrishna6044
@muralikrishna6044 Жыл бұрын
Sir here in interface we had created findByName+(Containing) but i don't know the purpose of containing key could you please let me know...
@Javatechie
@Javatechie Жыл бұрын
Do you know CONTAIN OR LIKE operator in SQL? CONTAINS is a predicate used in the WHERE clause of a Transact-SQL SELECT statement to perform SQL Server full-text search on full-text indexed columns containing character-based data types.
@muralikrishna6044
@muralikrishna6044 Жыл бұрын
@@Javatechie got it sir thanks a bunch
@clipy5757
@clipy5757 Ай бұрын
What if we want to create course separately and then want to assign them to student ?
@anuraglakhara5167
@anuraglakhara5167 Жыл бұрын
Hello sir If we want to map previously saved courses to a new student then what should we do?
@Javatechie
@Javatechie Жыл бұрын
Yes simply pass student with list of existing courses
@maheshmali7015
@maheshmali7015 Жыл бұрын
@@Javatechie Hello Basant Sir.. When I am trying to create new student record with existing course details, duplicate records are inserting in course table..
@Javatechie
@Javatechie Жыл бұрын
Do save and update instead of direct doing save
@sudheerkumar-tp1mg
@sudheerkumar-tp1mg Жыл бұрын
Hi Basant sir, I have one interview question, how do we maintain @transactions between 2 micro services with 2 different dbs.
@sudheerkumar-tp1mg
@sudheerkumar-tp1mg Жыл бұрын
One interviewer asked me, please suggest any video in our channel
@Javatechie
@Javatechie Жыл бұрын
You need to use saga design patterns
@sudheerkumar-tp1mg
@sudheerkumar-tp1mg Жыл бұрын
Thank you very much basant sir, for your quick reply.
@Javatechie
@Javatechie Жыл бұрын
Please check the tutorial below 👇 1. kzbin.info/www/bejne/l4DIn2RjpbZ_kMk 2. kzbin.info/www/bejne/bICYmn1thbCIntU
@VIKASROYv
@VIKASROYv 10 ай бұрын
Everything works perfectly, But there is a problem While I am trying to save only students not courses It is not able to save. Is there a work around for those scenario.
@Javatechie
@Javatechie 10 ай бұрын
Can you change the cascade type and try once
@VIKASROYv
@VIKASROYv 10 ай бұрын
No Luck@@Javatechie , Failed to evaluate Jackson deserialization for type [[simple type, class com.example.fts.erp.entity.Student]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'defaultReference': back reference type (`java.util.Set`) not compatible with managed type (com.gradlic.fts.erp.entity.Student)
@madhusamala3921
@madhusamala3921 Жыл бұрын
Will this code works if other student buys the same courses , it is throwing me 'detached entity passed to persist'
@Javatechie
@Javatechie Жыл бұрын
Please change the cascade value to persist
@mridulrajbhar12
@mridulrajbhar12 Жыл бұрын
@@Javatechie it doesn't works
@codingsunday3707
@codingsunday3707 Ай бұрын
How can we fetch Courses that student is enrolled in ?
@Javatechie
@Javatechie Ай бұрын
You need to trigger join query
@GameCrawl
@GameCrawl 3 ай бұрын
May I know where is your OneToMany tutorial?
@muralikrishna6044
@muralikrishna6044 Жыл бұрын
Sir once hit the get button i got response only student elements I'm unable to see course elements but course elements stored in course database table pls let me know.. 😊
@Javatechie
@Javatechie Жыл бұрын
What is the FetchType you have set , make it EAGER
@muralikrishna6044
@muralikrishna6044 Жыл бұрын
@@Javatechie lazy sir
@muralikrishna6044
@muralikrishna6044 Жыл бұрын
@@Javatechie sir after changed also getting same error only able to see student elements..
@muralikrishna6044
@muralikrishna6044 Жыл бұрын
@@Javatechie as well posting elements while getting error like unsupported media type but i had added only json media type
@Javatechie
@Javatechie Жыл бұрын
Please check the code that I have shared and try to find out the difference
@pradeepkec
@pradeepkec Жыл бұрын
There will be an issue if u use mappedby in the child entity , if u delete the student, all the cource associated to students will also be deleted .
@Javatechie
@Javatechie Жыл бұрын
Is it let me check
@itsnarahari
@itsnarahari Жыл бұрын
Bro I want to tell you something Today's days even If we are using Spring boot and JPA they are not using that they are using database queries only they are not allowing us to use jpa features like onetomany manytomany I didn't understand these 🤔 y they are doing like these
@Javatechie
@Javatechie Жыл бұрын
Usually people follow a microservice pattern where you should have a single database per service that is the reason they avoid using association mapping
@sureshmanne7245
@sureshmanne7245 Жыл бұрын
26:30 is it mandatory or neccesary that we create a new 3rd table with @ManyToMany mapping? Can we not use @ManyToMany without a 3rd table?
@sureshmanne7245
@sureshmanne7245 Жыл бұрын
Also can you tell how the methods findStudentsContainingByName & findCourseLessThanPrice fetching the data with out any JPA query written in Repository classes ?
@matheuspegorari1094
@matheuspegorari1094 8 ай бұрын
Yes it is mandatory, Many to Many relationship demands a intermediary table.
@vengateshkannan3297
@vengateshkannan3297 10 ай бұрын
"status": 415, I am getting unsupported media type error in postman
@Nab-dh7bl
@Nab-dh7bl 2 ай бұрын
how did you solve it, i have the same problem thanks in advance!
@srikarthickk9708
@srikarthickk9708 19 күн бұрын
Did u fix it?
@vibhutimaheshwari7350
@vibhutimaheshwari7350 8 ай бұрын
How is this many to many? This is rather one to many (or many to one). For one student you are saving many courses but haven't demonstrated that for one new course added to table, many students are enrolled. Also, you haven't demonstrated what happens when both santosh and basant have the same course in their enrollment list. (I tried it out and observed that two copies of the same course are getting stored instead of just one)
@Javatechie
@Javatechie 8 ай бұрын
First of all this is one to many unidirectional and whatever you caught is a good point will check and update here
@meet6712
@meet6712 Жыл бұрын
can you please give coupon for spring data jpa course
@Javatechie
@Javatechie Жыл бұрын
Didn't get you @Meet
@meet6712
@meet6712 Жыл бұрын
@@Javatechie discount coupon for your udemy course of spring data jpa
@Javatechie
@Javatechie Жыл бұрын
I don't have any udemy course buddy. I am giving live class on unacademy and yes coupon is available. Please reach me on javatechie4u@gmail.com for any queries
@amitkadam2354
@amitkadam2354 3 ай бұрын
I am very much confused with mappedBy
@knmuralimurali2922
@knmuralimurali2922 10 ай бұрын
{ "productsdetails": [ { "id": "1", "pname": "Product 1", "pquantity": "10", "punitcost": "100" }, { "id": "2", "pname": "Product 2", "pquantity": "20", "punitcost": "150" } ], "productscategory": [ { "cid": "1", "Pcategory": "Category A" }, { "cid": "2", "Pcategory": "Category B" } ], "productscolour": [ { "rid": "1", "Pcolour": "Red" }, { "rid": "2", "Pcolour": "Blue" } ] } i need to post values in json can you give me code
@djv650
@djv650 Жыл бұрын
Following the same code setup but getting this error when running: Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: Can anyone help me on this?
@rakibulhasan1923
@rakibulhasan1923 Жыл бұрын
when I used The @JsonManagedReference and @JsonBackReference annotations the data fetching success but did not save data. but without those annotations data saving success but not fetch data
@Javatechie
@Javatechie Жыл бұрын
With this annotations Post is not working? Do you mean the same
@learningjavawithme915
@learningjavawithme915 Жыл бұрын
@@Javatechie yes
@beytullahyayla7401
@beytullahyayla7401 Жыл бұрын
Hi, did you find the answer
@Javatechie
@Javatechie Жыл бұрын
Are you also facing same issue?
@beytullahyayla5971
@beytullahyayla5971 Жыл бұрын
@@Javatechie yes
@sanjaykumarnayak8877
@sanjaykumarnayak8877 Жыл бұрын
Here One thing I tried is that ,after giving @JsonManagedReference and @JsonBackReference I am unable to save the data now it is saying Media type not supported, why so?
아이스크림으로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 54 МЛН
Khó thế mà cũng làm được || How did the police do that? #shorts
01:00
#12 Hibernate Tutorial | Mapping Relations Theory
13:14
Telusko
Рет қаралды 296 М.
Part 1 - Relationships in Hibernate Using SpringBoot OneToMany and ManyToOne
9:08
Use Case JPA Hibernate Spring Data Many To Many Case
1:19:39
Professeur Mohamed YOUSSFI
Рет қаралды 13 М.
1$ vs 500$ ВИРТУАЛЬНАЯ РЕАЛЬНОСТЬ !
23:20
GoldenBurst
Рет қаралды 1,6 МЛН
Simple maintenance. #leddisplay #ledscreen #ledwall #ledmodule #ledinstallation
0:19
LED Screen Factory-EagerLED
Рет қаралды 29 МЛН
Опять съемные крышки в смартфонах? #cmf
0:50
ИГРОВОВЫЙ НОУТ ASUS ЗА 57 тысяч
25:33
Ремонтяш
Рет қаралды 351 М.