Spring Data JPA Relationships - ManyToMany, ManyToOne & OneToMany

Sdílet
Vložit
  • čas přidán 7. 07. 2024
  • Buy me a coffee: ko-fi.com/boualiali In this video you will understand how to create relations and mapping between you java entity and ensure the same class or database diagram that designed at the beginning.
    You will learn the OnToOne relation with unidirectional and bidirectional mapping.
    You will learn the ManyToOne and OneToMany relation and it works it details
    You will learn the ManyToMany relation and how to implement it
    #spring #learning #springboot #springtutorial #springsecurity #developpement #java #arraylist #linkedlist #springdatajpa #querybuilder
    Don't Forget to
    ===========================================
    💯 Subscribe to the youtube channel
    💯 Join our Discord Community - / discord
    💯 Join our Facebook Group - / 589612651142975
    Table of content
    00:00 Introduction
    01:22 Join our community
    02:05 Presenting the new class diagram
    06:13 @OneToOne relation
    14:24 @ManyToOne and @OneToMany relation
    22:06 @ManyToMany relation
    29:07 What comes next?

Komentáře • 126

  • @BoualiAli
    @BoualiAli  Před 10 měsíci +1

    Join the Micro Services course waiting list and get and get an exclusive *EARLY-BIRD discount*
    aliboucoding.ck.page/d0f9317e13

  • @DiplomasizKodcu
    @DiplomasizKodcu Před 5 měsíci +11

    How did I not seeing your videos before? Did you know your the only one who explained entire stuff? I will watch your entire list and share your account for those who need. Thanks a lot for great content.

  • @landry_john
    @landry_john Před 11 měsíci +3

    Thank you for that great lesson about Spring Data JPA Relationships.

  • @hazel-BKJ
    @hazel-BKJ Před 3 měsíci

    Your video is beyond reproach, from theoretical concepts to practical application. Thank you very much for the content, Mr Ali, which is full of pedagogy and enables me to make progress as a beginner with the spring boot framework.

  • @mohamedhediaissi598
    @mohamedhediaissi598 Před 26 dny

    Thank you so much for this video !! It really helped me a lot !!

  • @matthewoluwadare
    @matthewoluwadare Před 11 měsíci +3

    Very very good explanation, well simplified, great job!

  • @user-kx9tk3qf4k
    @user-kx9tk3qf4k Před 3 měsíci +1

    the only video that made me understand the concept great work

  • @mamadoumorobah1777
    @mamadoumorobah1777 Před rokem

    Merci beaucoup pour le partage de savoir

    • @BoualiAli
      @BoualiAli  Před rokem

      My pleasure
      Don’t forget to share the video 😄

  • @messironaldo1708
    @messironaldo1708 Před rokem +1

    That was soo helpful. Thank you!

  • @ines_edim
    @ines_edim Před 8 měsíci +3

    1. You have good well drawn out diagrams and visualizations, it's very helpful
    2. Your explanations shows that you have solid fundamentals [Coding + SQL]
    3. You gave Practical and Relatable examples.
    4. Kudos to you. I am coming from a C++ background, and I appreciate the attention to details in this video. I'll be sure to watch the playlist.
    You've got 1 new subscriber here.

    • @BoualiAli
      @BoualiAli  Před 7 měsíci

      Great and detailed feedback.
      Happy you liked it!

  • @markotomic4368
    @markotomic4368 Před měsícem

    Thank you Ali for this wonderfull explanation!

  • @satyamsc
    @satyamsc Před rokem

    Nice.. Easy to understand. !!!.

    • @BoualiAli
      @BoualiAli  Před rokem

      Happy to know your feedback 😁

  • @supun_sandaruwan
    @supun_sandaruwan Před rokem

    awesome video 🔥 Thank you

  • @fernandoereno5597
    @fernandoereno5597 Před rokem

    Thank you!!!

  • @sphesihlemanuel2933
    @sphesihlemanuel2933 Před 11 měsíci

    Great video, this was very helpful

  • @trinhlong2665
    @trinhlong2665 Před rokem +1

    Thank you Bouali Ali. Would be glad if you can complete this project by creating some Rest Controller class. I hope to see you have more videos about this course.

    • @BoualiAli
      @BoualiAli  Před rokem +3

      I already have videos about that. Check the playlist Spring workshops

    • @abhinavjain8613
      @abhinavjain8613 Před 8 měsíci

      Please add link

  • @AshishRohillax
    @AshishRohillax Před rokem

    Thanks a lot, really helpful

  • @khaledoueslati66
    @khaledoueslati66 Před 2 měsíci

    Very Nice video keep the great work up 🙌🙌

  • @okeowoaderemi
    @okeowoaderemi Před 5 měsíci +1

    Amazing Tutorial I just joined your discord

  • @mohamedoueslati462
    @mohamedoueslati462 Před 11 měsíci

    Good job , Thanks Ali :)

  • @mursalin3698
    @mursalin3698 Před rokem

    well done

  • @angsh
    @angsh Před 9 měsíci +1

    Gold!

  • @wassimsakri8654
    @wassimsakri8654 Před rokem

    thank you

  • @amenimensi8817
    @amenimensi8817 Před 2 měsíci

    good job

  • @genusleemapedze1177
    @genusleemapedze1177 Před rokem +5

    Would be glad if you can create maybe a single end to end application with spring security included in the app and also learning more about the DTOs
    e.g a blog app with several roless

    • @BoualiAli
      @BoualiAli  Před rokem +2

      I will consider this idea.

    • @solowolf5304
      @solowolf5304 Před rokem

      @@BoualiAli yes plz do consider this

    • @inihood
      @inihood Před 11 měsíci

      @@BoualiAli Please do it

    • @BoualiAli
      @BoualiAli  Před 11 měsíci +1

      @@inihood i already published a spring security video

  • @solowolf5304
    @solowolf5304 Před rokem

    New viewer here. I hope 10K subscribers would be a good motivation for you to bring more good content like this !!

  • @user-vj8vl8yt6q
    @user-vj8vl8yt6q Před 24 dny

    Perform CRUD operation in above relationship

  • @emonadriane5525
    @emonadriane5525 Před rokem

    Thanks really @Bouli ..can you please make a video/tutorial on making an e-commerce API including uploading product image plus attaching the different prices. Or maybe a destinations API

    • @BoualiAli
      @BoualiAli  Před rokem +1

      I really would like to, bit this gonna take so much time. I hope I can do it one day

    • @emonadriane5525
      @emonadriane5525 Před rokem

      ​@@BoualiAli Really thanks. Stay blessed!

  • @MFM88832
    @MFM88832 Před 2 měsíci

    Good video but it would be more informative if you showed Crow's foot notation in your visaulisation diagram.

  • @AntonioCabralNumberOne
    @AntonioCabralNumberOne Před 3 měsíci

    Thank you once again for your dedication with your beautifully explained videos!
    I have one question about ManyToMany relationships:
    If you need an extra column in the relationship table between (in your case) the Employee and the Mission tables, let's say e.g. a "created_at" column, how would you add it, set it and get it?
    Thank you in advance!

    • @Mr_Mazemaker
      @Mr_Mazemaker Před 2 měsíci

      Hi there! So, I'm about a month late, and I'm also just getting started with coding, but I solved this situation last week.
      In ManyToMany relationships, the intermediate table is generated automatically, which means you can't easily add more attributes without modifying the relationship itself.
      The only way I found to resolve this involves actually not using ManyToMany but setting up two OneToMany relationships between employees/missions on the "One" side, linking them to a third entity that contains both IDs along with any extra attributes. Using an example, this would be represented by the EmployeeMission class, which includes employeeId, missionId, and createdAt attributes.
      As Ali shows here, for OneToMany relationships, it's important to specify the "mappedBy" annotation, and I also found it helpful to specify the fetch type.
      I hope my explanation is clear and helpful. Have a great day!

  • @mohamedwiem4965
    @mohamedwiem4965 Před rokem +1

    Amazing content as well, just I wanna ask if the integration of database and diagrams you use exists in STS or intellij community edition or just in intellij ultimate !

    • @BoualiAli
      @BoualiAli  Před rokem +1

      Happy you liked it.
      Not sure about sts, but for Intellij it is just ultimate version

  • @elliotsaii7904
    @elliotsaii7904 Před rokem

    that's was awesome video

  • @kevinameda2711
    @kevinameda2711 Před rokem

    Hi @Bouli for the great tutorials. Could you please make a video about spring security with oauth will appreciate thank you in advance

    • @BoualiAli
      @BoualiAli  Před rokem

      Already have one about spring security. Check it in the same playlist

    • @kevinameda2711
      @kevinameda2711 Před rokem

      @@BoualiAli I did and it is an absolute honour to even talk to you. Where can I reach u out to talk further?

    • @BoualiAli
      @BoualiAli  Před rokem

      @@kevinameda2711 the honor is mine
      You can reach me on our discord server

  • @apexyu164
    @apexyu164 Před měsícem

    in all this relationship, which one will auto ipdate the table if one insert happen

  • @nourhenjridi7963
    @nourhenjridi7963 Před 4 měsíci

    SVP comment vous affichez le diagramme de classe représentant les entités dans votre projet Spring Boot avec une base de données ?

  • @plowman5260
    @plowman5260 Před 3 měsíci

    Thank you for your help

  • @parmodprajapati
    @parmodprajapati Před 2 měsíci

    Hi,
    When we go for Unidirectional mapping and which is best according to requirement?
    We can also set the employee id to the address table so why should we address id set to the employee table.

    • @BoualiAli
      @BoualiAli  Před 2 měsíci

      It depends on the logic and needs for your application

  • @fumano2679
    @fumano2679 Před rokem

    I have a problem when inserting an entry with the many-to-one relationship. Lets say i have created a department with the id of 1 in my database and now want to create a employee. I a few cases to do this. 1. By placing the department_id = 1 int the json, 2. by placing the department = 1 in the json, 3. by placing the department equal to the department json from my db entry, 4. by getting the department object from an api call and then employee.setDepartment(department). None worked. Is it not possible to create an Employee by referencing the department id?

    • @BoualiAli
      @BoualiAli  Před rokem

      when creating an employee you need to set the department object and pass the department is
      Here is an example
      Department dep = new Department()
      dep.setId(departmentId) // the one you get in your request / object / param (depends how you get it)
      Employee emp = new Employee()
      emp.setField()
      emp.set....()
      emp.setDepartment(dep)
      repository.save(emp)

  • @mohamedwiem4965
    @mohamedwiem4965 Před rokem

    thank u ! employee_mission was automatically generated in our database , should we create employee_mission class in our entity package or is not necessary ?

    • @BoualiAli
      @BoualiAli  Před rokem +1

      if you don't have any extra fields, you don't need to

  • @zqgAFf
    @zqgAFf Před rokem

    Thank you so much for such a nice clean video , However I made the simlar entities with one to one bidirectional , when I check the database , it does have each other id as foriein key ,however my app won't start again , but if add something like mapped by employee and add cascade = cascadeType.All , then there won't be two foreign key , there is only one address_id ,but the other foreign key won't appear from the other table ....I am confused

    • @BoualiAli
      @BoualiAli  Před rokem

      Why your app is not starting after adding one to one?
      Can you provide some logs in the discord server (screenshots) to make easier to understand

    • @zqgAFf
      @zqgAFf Před rokem

      @@BoualiAli
      Yeah I noticed on one to one bidirectional it is better not to provide mapped by on one entity otherwise,otherwise there’s only one foreign key like unidirectional

  • @ernanibatista1806
    @ernanibatista1806 Před 4 měsíci

    Tks but when we insert in the table employee , adress_id retuning null

  • @samirbenaziz520
    @samirbenaziz520 Před měsícem

    salem khouya ya3tik sa7a 3al vidéo
    juste question : OneToMany wel ManyToOne dima relation binéthom unidirectionnelle ???

  • @adelteyeb2427
    @adelteyeb2427 Před rokem

    Thinks can you make a video about MVC architecture

  • @jopadjr
    @jopadjr Před rokem

    46th...Thanks

  • @osasereuhunmwangho5293

    Thank you Bouali Ali. This is very concise and really gainful for me. For the one-to-one relationship, in what context is the relationship uni-directional or bi-directional?. Thank You

    • @BoualiAli
      @BoualiAli  Před rokem +1

      Unidirectional Relationship: This means that one entity has knowledge of the other, but not vice versa. In other words, if you have two entities, say EntityA and EntityB, and if EntityA has a reference to EntityB (but EntityB does not have a reference to EntityA), then it's a unidirectional relationship.
      Unidirectional relationships are simpler and less complex, and are suitable when there is no need for the referenced entity (EntityB in our example) to know about the entity that is referencing it (EntityA). For instance, if you have a User entity and a Profile entity, and each User has one Profile, but a Profile does not need to know which User it belongs to, you can model this as a unidirectional relationship from User to Profile.
      Bidirectional Relationship: This is when both entities are aware of each other - EntityA has a reference to EntityB, and EntityB also has a reference to EntityA. This kind of relationship is a bit more complex due to the need to keep both sides of the relationship in sync.
      Bidirectional relationships can be useful when there is a need for both entities to be aware of each other. For example, if you have a Customer entity and a BankAccount entity, and each Customer has one BankAccount, but a BankAccount also needs to know which Customer it belongs to, then you would model this as a bidirectional relationship.

  • @chokribenrejeb8115
    @chokribenrejeb8115 Před rokem

    Thank you,. how we can achieve ManyToMany relationship whene the join Table should have some data.

    • @BoualiAli
      @BoualiAli  Před rokem +1

      You need to create an extra entity that will hold the relation and the extra fields as well

    • @kamilleskamilless8113
      @kamilleskamilless8113 Před rokem

      @@BoualiAli could you please create video about that?

    • @BoualiAli
      @BoualiAli  Před rokem

      @@kamilleskamilless8113 about what exaclty?

  • @mugenzideodate5733
    @mugenzideodate5733 Před 10 měsíci

    Can you help us, full project by using spring boot multipe tables like 3 tables from database
    on back end and fetch data to front-end in single table?

    • @BoualiAli
      @BoualiAli  Před 10 měsíci

      aliboucoding.com/courses

  • @dorrakadri1474
    @dorrakadri1474 Před rokem

    can u explain the cascade types

    • @BoualiAli
      @BoualiAli  Před rokem

      Will note that for the next video

  • @haobui8913
    @haobui8913 Před měsícem

    Can anyone tell me what is the function of MappedBy????

  • @apexyu164
    @apexyu164 Před měsícem

    How to add database to spring in right panel?

  • @orestdymarchuk2910
    @orestdymarchuk2910 Před rokem

    When you create an Entity, you created an Integer field with the "id" annotation.
    Can you please clarify why this field should be an Object type and not a primitive one?

    • @BoualiAli
      @BoualiAli  Před rokem +3

      Object by default is null
      Premitive like int by default 0
      You null on the id to help hibernate well manage the entities.
      Checkout my new course about spring data jpa and you will understand all this details and master this topic
      aliboucoding.com/courses

    • @sureshattimarad1796
      @sureshattimarad1796 Před 9 měsíci +1

      Bacause we use ArrayList type field to hold MANY objects,
      Since ArrayList implements List interface that SUPPORTS only OBJECT TYPE not the primitive type

  • @thanhninh8677
    @thanhninh8677 Před 11 měsíci

    Can you give me the link of the next video , i can't find it

    • @BoualiAli
      @BoualiAli  Před 11 měsíci

      Spring Data JPA | Hibernate
      czcams.com/play/PL41m5U3u3wwkS8BU0fIeRQwY3hK4VlFlX.html

  • @amineessaoud1493
    @amineessaoud1493 Před 2 měsíci

    Does OneToMany & OneToMany exist in Spring Boot ?

  • @arslanoulhaci9965
    @arslanoulhaci9965 Před 2 měsíci

    VIZU ALI ZATION visualization

  • @Razes9ooo474lO4
    @Razes9ooo474lO4 Před měsícem

    employee and department uni directional or bi-directional , department table doesnt have any fk of employee

  • @aboubacardiaby3700
    @aboubacardiaby3700 Před 6 měsíci +1

    Salut Mr Bouali j'ai une question pertinante pour vous.
    le faite de créer une liste lorsqu'on a une annotation @OneToMany dans une classe permet de faire quoi concrètement

  • @stevegatspaul
    @stevegatspaul Před rokem

    Hey,
    Amazing content as always, but I have a question about how to use this in "@Repository public interface EmployeeDao extends JpaRepository", for example :
    findByEmployeeId and get: Employee Info and his address and his Mission and his Department.

    • @BoualiAli
      @BoualiAli  Před rokem +1

      Hi, can explain your question? I didn’t understand

    • @stevegatspaul
      @stevegatspaul Před rokem

      @@BoualiAli how to use join query in spring data jpa, use Employee_Id and get: Employee Info and his address and his Mission and his Department. thank you in advance for your reply. 😀

  • @KlanBr1
    @KlanBr1 Před 11 měsíci

    i am doing this tutorial with pgAdmin 4, and the relation ONETOONE, it's looks like ONETOMANY on my DER

    • @BoualiAli
      @BoualiAli  Před 11 měsíci

      How is that possible?
      Can you share some screeshots and code snippets on the discord server?

  • @imedhr1814
    @imedhr1814 Před rokem

    thanks but you did not explain the difference between unidirectional and bidirectional in @OneToMany @ManyToOne !!

    • @BoualiAli
      @BoualiAli  Před rokem

      It was already explained in the beginning of the video

  • @miloudseo7883
    @miloudseo7883 Před rokem

    Je crois au niveau entity employee tu doid crée comme ceci:@ManyToOne(mapping="employee")
    Private collection departmant;

    • @BoualiAli
      @BoualiAli  Před rokem

      Pourquoi?

    • @miloudseo7883
      @miloudseo7883 Před rokem

      @@BoualiAli suivant la cardinalite que vous mentionné dans votre diagramme de class.

    • @BoualiAli
      @BoualiAli  Před rokem

      @@miloudseo7883 peux tu expliquer plus?

    • @miloudseo7883
      @miloudseo7883 Před rokem

      @@BoualiAli clé étranger de la table employee doit etre dans la table départemant car un employee appartient à plusieurs départemant, et chaque départemant ayant une seul employee(ma raisonnement suivant la cardinalité que vous avez mentionné, en uml en raisonne comme ca)

    • @miloudseo7883
      @miloudseo7883 Před rokem

      @@BoualiAli je crois tu dois changer la cardinalité entre table employee(*) et departemant(*) to ManyToMany