Spring Data JPA Relationships - ManyToMany, ManyToOne & OneToMany

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back I'm super happy to see it again here so many of you have been requesting this video the one that we will see together today and this video is the next up of the previous one where we saw together how to implement entities using springboard data GPA in this video we'll see how we can manage the different relationships between between entities like one-to-one relationship many to one and many to many we will see how we can handle unidirectional relationships and bi-directional relationships my name is Ali and a senior software engineer and if you're new to my channel just go ahead and subscribe so you can get notified every time I publish a new video also if you like my content don't forget to smash the like button and give me a thumbs up so I can keep continue on recording this video for you guys and one last thing just leave in the comments what you want to see next this really really really helped me a lot providing you more content like that and do not forget to join our great Community the great developers community that you are building right now you will find all the links in the description join our Discord server you can also join our Facebook group so you can provide and get help every time you need also you can have a chat with me directly on Discord because most of the times I go live on Discord and you can have a live chat together so see you there guys foreign [Applause] [Music] before we start coding let's first have a look on the extended class diagram or the database diagram that we will work on so as you remember from the last video when we introduced how we can create entities and tables using a Java program or using spring data GPA now we will see how we can Implement relationships using annotations and here let me just explain this schema here so we said or the last time we started already with an employee table and now we want to extend this and the employee will have an address so instead of storing all the address data in the employee table so what we will do we will just extract them to a different table and the relation between the employee and the address would be one to one why because the employee has one address or like he lives only in one place and the same house or that house is only like has only one person so means one employee has one address and one address only belongs to one employee so this is the first one the second one and yeah before we move to the second relationship what we will see in the one to one relationship is we will see how to implement one directional or unidirectional relationship and how we can Implement bi-directional relationship so next one will be uh one too many and many to one relationship and this is we will this will see it between the employee and the department so here we have one employee is assigned to one department so in in the company or like in the organism where this on play Works uh he can be only assigned to one department so let's say it department or let's say Java Department and the Java Department can have multiple employees like zero or more uh more employees and why we have zero here because we can create a department without without uh having employees in this assigned to this department okay so next one next one will be the many to many relationship and the menu to many relationship we will see it using this table we will call it mission so an employee can have multiple missions and in one mission in one Mission we can see or we can have multiple employees for that one let's say or let's just take a small a small example so one imp okay let's so let's start from the mission side so in the mission let's have a mission of mounting uh some furniture for example okay and to mount this furniture we need many employees so one in one Mission we have many employees now from the employee side the employee can be assigned or affected to many missions so the employee inside the company he does not have only one machine but he has multiple missions so this is what we will see and we will create one by one the tables here so you know already from the past video how to create or how can we can transform this table into a Java code if not if you just if you just saw this video just go ahead and check it from the playlist all right so now let's go ahead and dive into our code okay so right now let's go ahead and start implementing the one to one annotation so I really like coffee and I need coffee to code okay so this is the project we developed we developed together in the previous video so if you want to know or if you just moved to this video just a reminder go ahead and check the previous one okay so here we will go to this package main Java and the models and the first thing to do is we want to create the address class so inside that package we want to create the address and here we will have the integer ID we also have the string Street name for example we will have a string or an integer house number and finally a private string the ZIP code so this is our entity now this is our first our Java model let's now add the required annotations Etc getter and let's say all arcs and no arcs Constructor so for the moment I will not need all this all arcs Constructor and also we have a default one so we can just remove them I just need a Getters and Setters annotation uh okay so first let's say this is an entity and as we explained before for the entity we need the ID annotation and let's say that it's a generated value Okay so now what we will do we will see how we can create a relation between this employee and address and we want to and we want it to be a one directional one-to-one relationship what does one-to-one unidirectional relationship means means this means that I want only one entity to be the holder of the relationship and the holder means that if for example I decide to to set this employee entity as the holder or the parent of this relationship so the foreign key will be created from the employee side otherwise if I wanted to be if I want the address to be the holder of the relationship so the the foreign key or the employee ID foreign key will be created from the address or in the address entity and then what we will see next we can we will see how I can also create a bidirectional relationship and I mean one to one bidirectional one relationship between address and employee so let's go ahead and make the employee the owner of the relationship I will create simply an object of type address here make sure that this is the the right package and they will just give it The annotation address so here as we say it's a one-to-one relationship okay and we need here to precise a join column and this John colon will have a name and let's call it address ID for example or address underscore ID or whatever you want okay so now this is it this is how we can create a one-to-one unidirectional relationship let's start the application and see what happens here so we wait just a few seconds and now we have the application I will go to my database and just refresh it now I should see a new employee or a new table called address and what you can do like using um IntelliJ you can go to the public schema or your database and just right click and here you can say diagrams and show visualization okay I guess I guess you know what I mean I guess you know what I mean okay so this is what we have here and as you can see we have this address table that contains ID house number street name and zip code as we defined in the entity and now we have also our employee let me zoom in a bit so you can see it how can I zoom in uh okay actual size all right anyways um okay so here we see that in the employee table we have now the address ID so this is a colon or a foreign key and as you can see here it's a foreign key and it's related to the address and here we see it the key or the key icon is in blue so this means this is a foreign key so now we have our one there or one or unidirectional one-to-one relationship between employee and address now I want to make it a b directional relationship so this means I want also to see the address ID in the address sorry not address ID but the user or the employee ID in the address table let's go ahead and do it so it's exactly the same thing so one to one with join colon address ID and here we have an object of type address I will just copy this two annotations and go to the address table and here I'm gonna create an object of type employee I'm gonna call it employee and I will add this relationship so now this one is going to be employee ID I will rerun the application and I will show you or let's try to actualize or refresh the data model so if I refresh data model I guess we have an exception right right here no we don't have any exception yes we have uh okay okay let's just go here refresh okay I'm gonna I'm gonna generate it again one second so refresh this one so we can get the changes diagrams and then show visualization all right so now as we can see here we can see that we have a double arrow and this double arrow let's make it maybe more visible for you let's put it here okay so we see now that we have a b directional relationship between our entity employee and address so here we see that from employee to address we have this one and its address ID and from address to employee we have also the employee ID and now we can see that we have the employee ID in our address table so this is the way all of this is how we can Implement a one-to-one relationship let's move on now and implement the one too many and many two one relationship between the employee and the department so this is our model just as a reminder and now I will go ahead and create this department entity so here I have a department and also we need the getter we need the setter annotation and entity so now we know how to create it I'm gonna create the ID identifier of type integer and I'm gonna say it's an ID and also it's a generated value and we said that the department has a string name or Department name and now let's understand how we can Implement relationship or one-to-many and many to one between the two entities or the different entities so the idea or the way like I'm gonna give you I'm gonna give you a hint how how you can remember or how you can understand this so uh all right let me try to explain this in a good way let's show back our diagram so we said here that one employee is assigned to one Department so one employee is assigned to one department and one Department can have many employees one Department can have many employees so and many employees here many employees can work in one department so if I go here and take the employee and here it's going to be private Department yeah here I'm gonna call it department and here we said one many employees can work in one Department many employees can work in one Department and here we need to add the join column that when we create a new employee we need to assign it to a department and here the name will be the department ID for example you can call it whatever you like so here we have many many employees are assigned to one department and from the Department side is one Department can figure out or can be shown in within many employees so let's at this point let's start and run the application first and and see if this is enough or not and see like let's try to understand if we can if we can just implement it this way or not I'm gonna refresh my database here let me close this one here I have my department and let me show the diagram the diagram visualization and here what we see we see that we have first of all let's move this up I'm Gonna Keep it a bidirectional relationship between address and employee but you can remove it I guess you you got you got the point so we see here that the employee the employee has now a new field called Department ID and we have a department and now they are both linked so this is what we did this is a one directional relationship like we just linked the employee to a department but if you want more consistency you have to go to the department and create a collection or a list it's up to you which one you prefer to use and here we will create a list of employees so this in in this case for example if you want I'm going to call this one employees and as we did here so here we said that many employees can work in the same Department and now the relationship from the employee side it's gonna be like as we saw just quick reminder here it's many two one so this way is going to be one too many and this one too many there is something which is really really really important which is an attribute called mapped by and this mapped by just never forget this otherwise it will have lots of exceptions here it should be exactly this name so go ahead and copy this variable name and put it here as the value of the map by now let's go ahead and start the application and meanwhile let me explain to you what's happening or how how you can remember all these relationships so like the easy way or the easiest recommendation is the way or the where you have the list or the collection directly think about using one too many because as I explained it's one Department to many employees means that I can have five employees that are affected to the to the same department so one Department to many employees from the other way it's many employees can work in one Department okay so when it when it comes to a single object use minute one and when it comes to a list of objects use one too many all right let's go ahead and check our database I'm gonna refresh this one and now we have three tables as we had before I'm gonna show the okay so this is our generated diagram and as you can see here uh actually I'm gonna just comment out this relationship this one-to-one relationship and the B directional and I'm gonna just keep it from I would just leave it here in the code but I will just keep it from from the employee side okay so now as I mentioned here we have a relation between our employee and department and as you can see we have a department ID here within the employee so when we create an employee we should have at least one department created so we can assign or affect this employee to this department so this is it next one we'll see the many too many relationship and how we can implement this relation between the employee and the mission okay now we will implement the many-to-many relationship between Mission and employee so actually implementing the many-to-many relationship is super super easy all we need to do is to have collections on both in both entities and each collection will reference the other one this means let's first create our mission entity so let's as always give it a Setter getter and the entity annotation and of course here let's create our ID let's create a string name like the name of the mission and finally let's let's let's make it in a duration or or the period of the mission here we need to provide also the ID annotation and the generated value so now we have our entity let's implement the many-to-many relationships so we said that we have a many-to-many relation between the mission and the employee so it's so and it's so easy it's quite easy all we need to do is to have a list of employees here and now in the employee side let's go ahead and create also a list of mission and let's call it missions so I will also add the many to many annotation on both sides so now okay now all we need to Define next is the owner of this relationship whether we want it to be employee or the mission let's let's say it's the employee who's gonna be the owner of of this relationship so once you define the employee so the other the other entity you need to add the method by and here it's going to be missions because the field name is called missions okay so now all we need to do in the uh like from the owner side here we need to define the join table annotation and the join table annotation has first of all a name let's call it employee Mission as a name and then we have to define the columns or the joint columns I'm missing a comma here so here join columns and how now we need to provide a join column annotation and this join column will have a name and this is going to be the name of the first part and the first part which is the entity itself so the entity itself means the owner of the relationship and here let's say it's gonna be employee ID also we need to define the reverse sorry not the reverse but the inverse join colon and it's also an annotation join colon and the name and this this this time it's going to be the other entity name so we will call it Mission underscore ID so that's it this is how we can Define or this is how we can create a mini to many relationship let's go ahead restart our application and check out or refresh our diagram and we need to see the same as we defined here all right so I'm gonna refresh now I should see five tables so we have the address Department employee Mission and employee Mission which is the one we just created right now and the employee Mission we have two columns which is the employee ID and the mission ID let me open the diagram for you I want visualization uh okay comment this out okay never mind so here is the diagram let me put it in full screen and scale it here so what we have is we have the same thing that we let me show yeah this one so we have it no well not ID but we have the employee Department address and machine let me put it in the same order we have address from here department down and employee in the middle and the mission is from this side so let's all right yeah this is optimizing nice nice I didn't I didn't know this too uh all right so here what we auto route okay okay so now it's Auto routing again uh okay so here also there are there are a lot of nice really really nice shortcuts where we can put them close we can extend them we can like yeah this is this is a nice Tool uh with entity J you can also have just the the entities and so on so forth okay now as we can see we have the same design of the same class diagram as the one we already designed here so here we have the employee and the mission it's a mini to many relationship and that's why we need to create a join table for that holding or carrying the employee ID and the mission ID so this is not all I'm gonna give you some announcements don't go away at the end of this video I really hope that you enjoyed the content that I'm providing and also I really hope that now you are a master of GPA relationships all right so the next video just give me a comment just leave a comment what you want to see next but I have something to propose to you and just comment out if it's fine or if you want me to provide a different one so what we will provide next is how we can use the criteria query to create custom queries and this is really really important especially when you want to perform complicated search actions and also a lot of special cases that you will see in that video so don't forget to subscribe to my channel and smash the like button so I can continue on providing this kind of content for you guys thanks a lot and see you in the next video
Info
Channel: Bouali Ali
Views: 36,940
Rating: undefined out of 5
Keywords: spring, jpa, data jpa, mapping, onetoone, one to one, spring data, many to one, manytoone, many to many, manytomany, class, entity, id, generatedvalue, generated value, column, persistence, repository, service, jparepository, jpa repository, crud repository, uml, class diagram, design, software, engineer, software engineer, java, jakarta, javax, spring boot, springboot, security, spring security, aliboucoding
Id: CvDS6DltIno
Channel Id: undefined
Length: 30min 15sec (1815 seconds)
Published: Wed Nov 02 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.