Spring Boot Many to Many Tutorial with Thymeleaf, Bootstrap and MySQL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the video springboard many to many tutorial with me nam hamming from code java.net through this video tutorial i will help you understand how a many-to-many entity relationship works in the context of a spring data jpi and hybrid framework and how to design and implement many too many entity real ships with various ukcs and you will learn how to code a springboard web application for managing a typical many to many relationship between users and roles in details you will learn how to code cloud relations on a many too many entities and ship something like this this is a great new user based energy when creating a new user we also can assign some rows to the user as well here this is a very common module for any web application and this is a user listing quest and you can see in the rows for each user displayed here and it will also learn how to implement the edit update and delete functions for this a very common very typical many-to-many entity relationship between users and roles so i will walk you through the process of coding server project based on these technologies spring boot web spring data jpi and hybnet need and spring test for emit testing time lift as a default template engine html5 and boot chat 4 and my sql database and as usual i'm using the fluorescent wearable programs for development in java development kit spring 2 3 and e mastercl community server and mysql workbench so i hope you will use the same and you can use the latest versions of these software programs in this video i will use the project which was developed in the video springboard one-to-many tutorial so it would be better if you already followed that video before coming to this one now let's understand how many too many entities chip is implemented in a database and the most popular and common way is having two main tables and an intermediate one like this for example the entity you can see between users and row is many too many meaning that user can be assigned for one or more rows and a row can be assigned to one or more users and we need to have an intermediate table user role that connects both main tables and in this intermediate table it must have the front keys to the ball tables as as you can see here the user id column here is a foreign key that refers to the primary key of the user table and row id column is the front key that refers to the primary key of the row table another example is the many-to-many entity between product and user in the context of shopping cart items user can choose on a mob products to the shopping cart and a product can be chosen by one or more users and the intermediate table is card items here and you can see in this intermediate table it can have a primary key an hr column and of course it must have front keys product id that refers to the primary key on the product table and user id is a front key that refers to the primary key of the user table so in the intermediate table it may or may not have the primary key it may have or may not have the hr columns but it must have the front keys that refer to the both main tables and the many too many anti-union chip can be implemented in java by having two ltd classes with the orientation from one side to another like this as you can see here we have two entity classes user and row here that mapped to the corresponding tables in the database and in the user entity class it has a set collection of raw objects here it refers to the raw entity here but in the row side here it doesn't have any reference to user so this is called uh unidirectional many too many between user and row and in case in the entity class row we have a reference to user like this users or set row here so in the raw entity class it also has a reciprocation to the user entity class here and the same from the user to the row so this is called a bi-directional management entity intensive and another way to represent many too many entities in java is having two main entity classes and an intermediate one like this in this example you see order and product and we have the intermediate entity class other details here that represents the many too many dnc between order and product a budget can be in one or more orders and another can contain all normal products and in the intermediate entity class it must have references to the both main entity classes you see order refers to order here and product refers to product here and you can also notice that it is actually uh two uh one too many indian ships the first one is one too many which in order and all the details and the second one is 120 between product and other details and this kind of implementation is suitable when we want to have some extra information about the leadership such as the quantity here and it is also suitable when we don't want to have any references on the main entity classes you see the other class doesn't have any reference to product and the product class doesn't have any reference to other class or is the intermediate class has the reference is aware about the many too many areas between order and product okay i think that's enough for the theory and we should come to the coding part in which i will show you how to implement the very common very typical uh many too many entities between user and raw and how to write image test for the cloud operations on this many too many association and this here spring 2 3 ide and i opened the project inventory app here this project was developed in the video springboard one too many tutorials so i recommend you to follow that video first and before coming to this section and this is my workbench here is the database inventory which is used by the inventory app and we use the movement to see the table data examine verify as data now in the project i create a new java package here not called java not user and create the first entity class which is a raw blast here and in this entity class we have only two fields that are name and id and generate a letter stance attached for this field select all generate okay and we need to use some annotation to mark this this an entity class and for the id field we need to use the id annotation with the generated value chat z equal generation type identity and for the field name that maps to the corresponding column in the database table we need to specify the maximum length of this view is there by our 25 characters and it is not nullable nullable equal force and the name of a row should be unique if it equal 2 that's it for the oral entity class and the second lcd class is asia and we'll also need to use the entity annotation and fuse id email password and for the id field we can copy this annotations and for the fused email and password yeah maximum length of email is 45 nullable force and email must be unique and for password the maximum length is 10 characters nullable false and it doesn't be quite unique and generate another stand setters no we need to have a reference to a collection of raw objects first yeah so we declare set collection raw objects the reason that we need to declare set collection because set doesn't allow duplicate elements so it is to ensure that the user has unique rows rows equal new hash set which is an implementation of set okay and then you can generate the letters and set us for this field so generate letters and set us select generate okay enough to uh represent many too many uh you'll see between uh user and raw uh here we are going to remain any direction many many you can see from user to row so here we need to use this annotation many too many from java i start persistent here and we need to specify the information about the joy table suggest the name of the table and the joy columns [Music] the table name light table name is underscore raw in the joy column columns one light column is usually underscore id and uh universe joy columns he's rowan's guardi so these side columns as a front key to the main tables you see you roll on the sky which would you name equal here okay so this is uh annotation uh we must use uh uh to invent many too many alienship in every direction uh many many from user to aurora now let's create a new unit test class for this user entity class so i create a new test class in the test source directory here new class is a repository test and i'm using the annotation data the api test provided by spring test then i want to run the test against the real database so i need to specify this annotation auto configure test database with the http replace equal none because by default spring test we use an in-memory test instead of the real database and on that other test we commit the changes to the real database so i use this annotation robot equal force here and in this test class i have a reference to the user repository interface user repository so we need to create a new interface for user repository here user repository and have this interface extends the jpi repository with the domain type is user and type of the id is integer okay and we have a reference to user repository here and the first test method public void test this year an empty method so when i run this method it we create the corresponding tables in the database based on the annotations in the entity classes here oral and user now let me check the database you can see it's an inventory database here refresh all and you can't see no users in a row tables here now let me run this test method in the user repository test class here okay the test has been successful and in the control view you can see uh various sql statements that created the table overall here table user here and the intermediate table user underscore row here and also [Music] create the content for the unique and front key here so first i would write and let me verify in the database and refresh the inventory database schema here and you can see there are two new tables user three new tables uh user role and user role got created here perfect right and we can see the columns of the row table id name columns in the user table id email password and columns in the intermediate table is our own as a two the front keys user id and raw id here if you right click on this user run table on the table and you can see the front keys here to the user table and row table here perfect right now let me update the first test method to proceed some raw objects into the database so the method name should be test correct or rows yeah and i create a new some more objects or rom admin equal numeral and modify the entity class row to have a contractor that text their own name slash zero and contractor using fields select all three different contractors the one that takes two parameter id name the second one text only name in the empty new argument conjecture okay and yeah i create a new raw object and the role name is administrator and the second row is raw editor the third row is a visitor is it okay and then we can use in a spring data jpa test class you can use the reference to the test entity manager test entity manager here this this year wrapper class on the jpi entity manager class entity manage the neutral wire yeah and then we can use the ndt manager to proceed the objects into the database unity manager proceed raw admin and plus or editor press it your visitor okay and first simplicity i don't write the code for the assertion statement just run the code inside the test method here and by default the test we succeed now around this test method and before that let me check the data in the row table here select browse and you can see the table is empty now now let me run this test method to proceed some raw objects into the database okay the test has been executed successfully and in the control conservative you can see the sql statements insert into row table three times here that inserted three uh rows into the row table let me check the database here and you can see there are three new rows all inserted in the raw table here administrator editor and visitor next i'm going to write the second test method that we proceed a new user object with a raw associated with a new or an existing role so the method name should be test career and new user with one row yeah and i will use the entitymanager here to retrieve an raw object from the database based on ide as you can see see in the database the raw administrator have has id1 here entity manager by my id here row.class primary key one and this return the new object or admin here and i create a new user object user user equal new user and i modify the user entity class to have some contractors here contractor using fields email and password okay here i can pass email and password here for example nam 2020 and then for the many so many association i can add a new run object to the user object here you can see we have set rows here and we can write a method public and row arrow object here this rows and row so here we can call user.addrew raw admin which he retrieved from the database yeah here and then we proceed this user object into the database using the user repository here there report save user and for the sake of simplicity i don't write the code for the assertion okay now let me open select rows from the user table here you can see this table is now empty and also in the intermediate table user row empty as well now let me run this test method okay and you can see the sql statement select to achieve run from the database based on id here and you can see there are two sql insert statements and the first one is to insert a new row into the user table and the second one is for inserting a new row into the user role table here let me check the database select arrows from the user table and you can see a new row inserted here correctly right and in the intermediate table here user role and you can see user id is one and row id is one exactly is a id of the user one and id of the row here so that's the very fundamental code for positing uh many too many rings between user and raw and next i'm going to write another test method that we perceive a new user object with two different rows okay so i can copy and modify this method test create new user with two rows here the first row is the row editor associated with the id number two in the database you can see raw id number two is editor and row id number 3 for visitor here so i retrieve two or objects from the database row editor and row visitor based on jid 9200 2193 and i create a new user and this time i use different email ravi not command dot gmail.com password rv1123 and you i add the row editor and row visitor to this user object so this demand chest how to proceed and use object with two rows or editor and row visitor okay and run this test method just create a new user with two rows okay and you can see the sql statements here to select statements for retrieving two rows from the database yeah and three one two three three insert statements that insert one user and two many too many association into the user role table here now let me check the database refresh the user table and you can see the second user what created here ravi kumar here id2 here and in the intermediate table and you can see there are two new rows all inserted for the user id 2 and the associated row id number 2 and number 3. perfect right next i will write another test method that assigns a row to an existing user so the method name is test percent row to existing user so i will retrieve the user with id1 from the database so i can use repo file by id 91 is for the user name sm here and i also reached the row id 2 which is a raw editor here and then i assign user a raw oral editor that's it and for this kind of operation we don't have to call the sub method the code will be terminated immediately because spring data jpa we commit the changes on the managed entity user here so now let's run this test method now let me check the usual room table and you can see the user id id one is assigned to only one draw id one here and let me run this test method we got an error here now default contractor for entity user okay so it's because in the user class we need to have um the default contractor uh with no algorithm here okay and run this test method again okay successful and you can see some sql statements here select user and select grow and insert new values into the internet intermediate table user row here okay and let me check the database request here and you can see now the user id one is assigned the second row with id number two here user id one here and draw id2 here the right right and the data in the user table when the wrong table remains unchanged here and i will write the next test method for remove removing a row from an existing user test will remove a row from existing user now i want to remove the row id 2 from the user id 1 here so i can got the user with anyone from the database and i will create a new raw object with the id so i modify the raw entity class to have a constructor that text only id here instead of retrieving a new oral object receiving an existing raw object from the database you can create a new object with a vivid id analysis raw id2 and then in the user method i write a new method that removes the row required remove row this rows remove row and for this to work we need to generate equals then hashcode method in the raw entity class generate equals then has code here for the id field here okay so in the test method here we can call user remove row for this row id 2 here that's it and now we can run this okay now we can run this test method now in the database you can see the user id one is aside two rows number one and number two here now you see now let me run this test method successful and you can see the sql delete statement at the end here delete from user role here let me check in the database here and refresh and and you can see the association between user id 1 and the row id id2 has been removed from this table perfect right so that's how to write code to remove a row from an existing user and here are the two important points which you should keep in mind when implementing many germany ltd and ship with spring data jpa and heimlich when using the many too many annotation the d4 cascade type is none that means any changes to one entity do not update the associated entities and the second point is the default fetch type is lazy that means the ritual of only do not load the associated entities i will write some code to explain these two important points now i will write another test method that we proceed a new user object uh associated with a new raw object okay test public roy test create new user and with new role so i create a new row object here overall sales person for example equal new or all sounds listen and create a news object user user one two three and i add the overall sales person to this user and i call on repo save user it is different from the very first test methods in which i receive raw objects from the database here yeah and now we are positing a new object with a new row which is not exist in the database now let me run this test method and you see the test method failed and let's see the error here we see the error object reference is an accept change in instance okay a lot of things in the reception structure is here but let me explain it because in the many too many real ship here the default cascade type is none that means any changes any changes to one entity the entity is the user here we not update the associated entities the associated entity is a raw object here so to make this work [Music] we should modify the many too many association in the user entity class here we need we must specify cascade type to one of these values here here i choose proceed so any um updates we update the associated entities okay that's it and now let me run the test method again here okay you see now the test is successful and let's see the sql statements you can see there are three insert statements here the first one insert in user the second one insert the neuron here and this is the effect of using the cascade type uh different than none here different than the forecaster the default cascade type for many too many is none so if we want to if we if we want hybrid to update the associated entities where we make changes to the one entity we must specify class case type here different than the default value which is none okay and let me check this database the row table first and you can see a new row or inserted here sales person with id4 here and in the user table and you can see the third user with ide for zone gmail.com here and in the intermediate table user row and you can see new association for user id 4 and raw id4 here perfect right that helps you understand the meaning of using cascade type in many too many entities next i'm going to write another test method to help you understand the default fetch type in many-to-many association so the best method is test web user and this method we will be cheap a new object from the database now for the user with id1 here and i want to print out the details of this user user email and i want to print out the rows aside for this user user red rows as you can see in the many too many annotation here in the user entity class here okay and i need to generate the two string method in this oral class in this first direction for name only and for simplicity just to return the name of the row so when using object in the system that i'm not pin line statement here row name will be displayed for raw object okay let me run this test net user method you see yeah it is successful and it brings in the console here email of the user and the oral name associated with this user which is administrator here it is because the user object here is still attached to the session in this method the file by id method return user object which is attached attached to the session and then we get the rows here now let me delete a command this line okay and you can see in the console view it executed sql statement for select user from user table and select grow from the row table here to reach retrieve the associated entities of the user entity now let me run this test method again run and you can see now it prints all easy email and you can see now it executed only one sql select statement that or egypt uh user from the database and you don't see the sql statement that receives a row from the database and that means the fetch type is none let me uh we text this user object from the session you can use the ndt manage entity manager detect as a user object from the session and then get the rows from this user object and run the test method again now run and she you see the test field it prints only the easier email and it favors this line and the line that receives the rows the associated empty entity from this new object and you can see in the unit error here failed to lazily initial collection of row here user dot arrows here that means by default as a fetch type of many too many [Music] association is lazy so when object becomes detect from the session we can't get the associated entities because it is lazily initialized now to make this code okay we need to update the many too many annotation here specify the attribute fetch equal to here so when let's equal sorry you are here the fetch type is eager here so when the user entity is loaded uh have net we also load the associated entities which are the rows assigned to that user as well okay now we can rerun this test method now see the test is successful when it prints email of the user and the associated role for that user here it is the effect of using the fetch type equal er here and when this user object is returned the associated entities arrows are loaded as well so when the user object becomes detected from the session we still can get the rules from the text entity here so that i think you understand the important points of many too many annotations with the cascade type and fetch type next i'm going to write the last test method for the many too many releasing between user and raw and in this test method i will remove user object public void as a mobile user and i don't want to use cascade and fetch tie other than the default values so i delete just use the default many too many with default fetch type is lazy and kaskata is none so yeah i can call the method of the user repository delete by id here i want to delete the user id number four id number four is the use zone at gmail.com here and run this test method and it should be successful you see the test is successful and it prints the sql delete statement here delete from the user table here and delete from the intermediate user role here let me check the database no more user zone in this table user here and normally the association of the user id4 here refresh you see it has been removed and the row table remains unchanged with four rows yeah perfect right next i will guide you how to apply a many-to-many union ship in developing cloud operations for the user management module in the screen boot web application as you can see when creating a new user we can also assign some rows to the user analysis and this is a listing user pressed here and we can see the rows for each user display like this and we can also edit or delete a specific user come back to the project and i will open the index the homepage of the application here as i mentioned earlier you need to follow my previous video springboard springboard one too many tutorials to develop this project so we will have this code okay this is a home test and i update it to have only hype links for the user management module here this is a this is a hyperlink url to display the list users users and this is a url for displaying the correct name user best create user okay and now let me start this springboard application and use chromazer to access the application home place here and you can see we have two links list users then list user and correct user here and next we need to create a spring mvc controller class for the user management module here user controller to use the annotation controller indicate that this is a spring receive controller and in this controller we need to have a reference to the user repository user report here and the first handler method is for this url it's last users that we display the user listing test public stream show user list determine the logic of your name is ask yeah and we need to access the model object of spring lvc here model and we call user repo or method that returns a list collection of the user objects here list users and we add this list user onto the model model and achieve you list users please use us and then we need to create the html paste for the user listing so we can copy and reuse the code of the categories paste here copy paste and modify the file name is users.html and modify the html code here the title of the page is list users the heading is user list and the columns id email id email and uh oroz we don't display the password email and rows and in the controller it sends the list correction user objects here list is just here so in the view best we use here we use timely dhh here i thread to each object in the list users here for each user here we display a new table row user dot id user not email and user not rows yeah and in zero class we have the twitching method here okay okay and click the list users link here and you can see the user listing paste that display and you can see there are two users uh email name is m ravi kumar in the role here role for the username is administrator and rows for the user ravi kumar is editor and visitor exactly the data we have in the database here is you can see [Music] in the oral assignment here you see the user id one which is named m is associated with the raw id1 and the user ravi kumar who has id2 is associated with the rows 92 and 93 in the rows here you see and update to have another column to show the edit and delete hyperlinks next and let's go to the good new user function so i will create a new handling it is a list best here under the best heading here we have hype link for creating a new user here than f equal career user the url is slash users slash new so in the user listing paste you can see we have the hub link that allows the user to create an user here okay and we need to quote the handler method in the controller class to show the user new form so we have mapping for the url users slash new public change so user so create a new user form and it returns the logic of your name to the user from cache and we need to access the springervc model object here and this is user interface of the create new user form you see we have the field user email password and rows that displays all the rows available in the database here okay so that means that we need to retrieve a list of pro objects from the database so we need to create a new raw repository here arrow repository interface and extend the api repository interface raw and it is here okay and in the controller class yeah we need to have a reference to row repository repository row repo here okay and here we use a row report here to create a list of rows objects yeah please roast here and put this list arose onto the model model and attribute the list rows list roast and we also need to put a new user object use a new user okay that's for the handler method that we show the create new user paste and for the create new user best we can reuse the code of the category form here so copy paste and then modify [Music] user let's confirm that html here use underscore form.html and change the base title to create new user first heading click new user when the url action of the form is slashed users slash save the object on the form is user and the first text field is the email user email and for html5 we can use the input type equal email and it maps to the field name email of the user object and we have the same button here okay now we can test create user here and we can see is a good new user form appears there and the next view is for the user's password so copy and paste modify the deep section here this here passed what and input time is that's what the field name is password okay you can see the view password here and then we need to display the list of rows it can be assigned to the user here so copy and paste this lib section rows here and we need to migrate through each arrow object in the list rows seen in the handler method list rows here so we use gh block here dhh for each raw object in the list rows here and then we create a input checkbox if input type equal checkbox to allow the user to select one or more rows to be assigned to the new user and th few equal to the field name is you know rows in the user entity class here you see rows here and then the text of the label for the checkbox is the value row name here yeah and then the value for the checkbox is the raw id raw dot id here okay and we can test refresh and we can see all the rows appear here see how many character editor sales person visitor exactly is uh rows in the database table here you see and we need to max this checkbox aligned to the lab okay so we need to use two in refresh and you can see we have a list of rows which can be assigned to the new user here and now let's go to the handler method for proceeding any user object into the database in the controller class here create a new handler method for handling the submission of the codename user form first mapping users slash save public string save user here's the object that maps to all the fields in the form and in this handler method we simply call user repo save the user object very simple in general return a logical video here we need we want to reject the user to the user list based last return this indirect string okay and then we can save the changes and ready to test the create new user function here now i create another user tina password when i assign some rows for this new user for example sales person and visitor and click save and you can see the new user tier obstacle and you're going to use a row resistance sales person here perfect right and let me check the database user table and you can see a new row for the new user tina samson here and in the user row table here we have new associations therefore anxiety five he's associated with raw id 3 and nobody for which uh visitor and sales person yeah perfect right and let me create another user achieve my password and now i want to assign the rows um editor and visitor to this user click save and you can see the new user gmail appears what created in eventual roles editor and visitor yeah perfect right and let me check the database again and you can see new user gmail here new raw association here next i'm going to show you how to invent the edit and update user function so i'll let the user listing quest to have the hype link edit yeah i did it left edit and this is a url slash is a slash edit and then follow the user id user id refresh the user list paste and you can see the edit have link next to each user here for example edit the username yeah okay you can see the url users slash edit slash and followed by the ide of the user here now let's arrive the corresponding analog method in the controller class here we get mapping edit and a place folder for the user id public string show edit user form we use a same form that allows the user to create an user so the name of the view here is useless conform and we need to use the mat variable annotation here to map the value of a placed error in the url here name is id and us map it to a variable integer id and a model object and then you we can use the user report method file by id to find a user object based on id here it returns an option object so here we get the value from option object user user equal and put this user object onto the model user user and because the edit form in the edit form we must show the rows aside to the user so we also need to retrieve a list of rows from the database and sell it onto the model yeah okay and changes then ready to test the edit user function here for example i want to edit the user ravi kumar here click edit and we're going to see the details of the user private command yes here and you can see the email and the rows here you can see it automatically select the checkboxes according to the assigned roles from this user editor and resistor try to edit is a tina zamason [Music] with the roast visitor and sales person here and you can see sales person and register are selected perfect right and try to edit the user gma edit and you can see the rows editor and resistor selected here and we need to update the user from a little bit here to display the correct heading here in case in case the user id is no that means creating new user and if user id is not no that means we are editing an existing user [Music] hidden here to store the value of the id of the user being edited in the edge field to the edit view obviously user object yeah okay select the changes and try to edit the user ravi comma here click edit and you can see the heading of the past is now edit user here view press last and you can see and put time equal hidden with value is the id of the user being edited here and create user and we can see the heading of the best is create new user okay now we can test edit function here it is ravi comma and now i want to assign another raw sales person to this user and for testing i need to enter the password again and click save and you can see the user ravi kumar has has been updated with the new rows aside sales person here perfect right let me verify that database yeah you know refresh the user role table and you can see there's a user ravi kumar with id number two here id number two and you can see the number two here is a side with three rows already two i need three id4 i need two 93.94 exactly the rows of the user ravi kumar here that that means the edit update user function is working perfectly and then we test another user update the user tina summerson here now i want to remove the cell the visitor row and add the row editor here click save i need to enter password click save and open teaser user tina zamason has been updated with the ros editor and sales person here and the raw vista has been removed from this user next i'm going to implement the delete user function which should be easy so we are in the user listing quest i add a new hype link that allows the user to remove the user from the database the url is users slash and delete slash and followed by the user id and you can see it sees a delete handling next to each user here okay and we need to write handler method in the controller class here so we can copy the signature of this channel method because they are similar method name is user in the mapping url is users slash delete slash and followed by the user id then in the handler method we simply call the repository method if the report.delete by id id and then return direct to the user list best redirect you just yeah that's very simple and we are ready to test the delete user function now i want to remove the user chima here ready seek here delete okay you can see that user has been removed and let me check the database user chima you see the user gmail has been removed actually and let me remove the user in ravi kumar you see it has been removed the request it has been removed next i will show you how to implement another typical many-to-many entity and chip in which an intermediate entity class is used to represent the audience as you can see in this class diagram the car item class represents the many too many aliens between product and user a project can be [Music] added by one or more users and the user can add one or more products to the shopping cart and using this intermediate entity class as many too many dnc is broken down into two one too many entities and chips the first one too many internship is between product and car items here meaning that the product can be added one or more times into the shopping cart and the second one too many alien ships is between user and car item here meaning that a user can add one or more items into the shopping cart and in the internet class you can see it has additional information such as id quantity besides as a reference to product and user here and this is a database design this is a product table user table and this is an intermediate table car items here it has its own primary key id and that is in the column quantity besides the front keys to product and user here so in the next few minutes you will learn how to write java code to illuminate many many ship and write some unit test methods for quite operating on this editorial ship okay come back to the project and in this project we already have two entity classes product and user yeah this is an entity product and this is entity user here and also in the database the product table here you see now let me create a new java package net.google.java.com shopping cart and create a new entity class track item and in this entity class we have a id few additional information about the quantity the number of products added to the shopping cart and a reference to the product and a reference to the user okay and we generate the letter stamp setters for this field so on generate and we use the entity annotation and use the table annotation to specify the table name which is different than the class name and cut underscore items and for the id field we use the same identifier annotations here and for the reference to product here because this entity class [Music] represents two quantum mechanisms between product and cut item between user and connect them so here we need to use the many to one annotation many to one annotation yeah that means the product can be added one or more times into this character and then we need to specify the zoid column which is the front key into the product table product let's go id and the same for the reference to the user here many to one meaning that a user can add one or more items and specify the design column and which is a font key to the user table here user let's go id that's it very simple and then we also generate some contractors here contractor using fused we need to have contractor for the fused quantity product is 90 okay remove the parameter id and we also need to have no argument before contractor required by hibernate okay and then we need to create a new interface for the car item depository here okay repository extends the jpa repository for the entity type is card actor type of ide is integer okay and write the test class we can name it as shopping cart test and we use the same annotations with the user test class here okay and here we have a reference or two wire to uh cut item repository here repo and we also have a reference to the test entity manager best entity manager tt and the first test method it is the empty test method and now i run this 10 meters and it will create the table in the database now you see in the database here we just product and user table here now let me run this test method okay the test has been successful and you can see some sql statements printed here that creates the table car items here with the columns id quantity draft underscore the userness id and content for front key refers to product and user here perfect right and let me verify in the database you see a new table card items you are created here you can see the columns id quantity the user id and let me enter this table and you can see the font keys here one key is open key to the user table another front key to the product table perfect right now let's update the first test method to test add item from database yeah we retrieve a product and a user from the database using the entity manager here file [Music] product dot class and let me check in the database we have the quota we don't have any product here don't worry now we update this project to i will update this project to add some products into the database index the homepage here okay and let me run this springbookweb application to add some products so it is it it is important to follow my video springboard i want too many tutorial first okay go to the whole mesh here yeah this cutaway list products yeah okay and i create a new product here for example iphone 12 price seven nine eight nine category electronics and diy os ios 12 camera 128 megapixel click save okay and we'll have one product with id1 here and now i in the desk method yeah i will get the product with id1 from the database product and for users i regard the user with id1 here entity means define user.class id one it item equal new cut item i want to add this product by this user with quantity one okay and then i call report same item and for the second simplity i don't write any assertion statement okay and now let's run this test method you see the test has been successful and you can see the sql statement here select product from block table select user from user table and insert a new row into the car items table here now let me check in the database in the car items table so that grows here and you can see there's a new row here quantity one pronoun id one is id1 perfect right now we [Music] add another project using the web interface here for the cutter we [Music] black seed number of seeds five in the engine 2.0 click save and we got the second product here now let me show you right another test method for adding a new item to the card and this time we use id we don't get objects from the database so the test method should be tested too ids yeah i create a new product object with the even id new protect yeah i create a contractor that text already here okay maybe uh two here i need two is for the new product when i see week here on the civic here and i also couldn't use the user object with the given id so i deserve it another contractor that text ready okay now use this now i choose a user with id5 tina's amazon here 95 and i create a new character relatively quality the quantity now is number two the product and user and proceed this character into the database save later okay and now we can run this test method [Music] okay run this test method see the test has been successful and we got the insert statement here and now you see there's no sql statement for selecting product and user because we pass the object with identifier id directory here so you see another way to proceed many too many uh real chip into the database let me check the table car items here and you can see the second row or inserted 92 quantity two block id to use id5 yeah perfect right next i will show you how to write another test method for adding multiple items at once through the card test click right test tuple items here and i'll also get user id user by id id one here product id one and voter id two and i will create another product could it product here for the category spot shot 10 detail length 30 [Music] and i have this third product with id3 here i need three here for that one up to wrap twist and then now i create three new connections object cut item and one equal view user is the same item two is troubled up two right three is for product three and quantity two three here okay now four eight five okay and then we can call the method repo or here and we can use in list off here item one item two item three okay and then you can run this test method multiple items you know jb test okay the test has been successful and look at the console view you can see it [Music] executed insert statement here sorry this should be item 3 so there are only two items added to the database you see you have i think when i need 384 for the product id on it two and quantity one and four yeah okay no problem so last week's use how to add multiple items uh like this this should be item three next let's write another test method for listing all items test test list so items can use the method of the repository it returns the this collection of character here list items and then we can use in lambda expression character and make sure that we have two generators here right to change for all the fields right okay and make sure that we have 2g method implemented in the product class we show only the product name and to string in the user class and we display only show only return on these user email here okay now we are ready to run this test method test list items or items and you can see it brings the one two three four card items yeah with id one two three four quantity product iphone under here user email here perfect right next let's write another test method for updating an existing item test public door test update item here we will keep a character from the database using entity manager here by item well we can use the cut item repository fly by id we want to add the connector with the idea one here we want to update the quantity so we set quantity we said quantity is 10 for example and you want to also change the product id from one to gravity yeah to block id3 so here we can also change the production product new product 3 here that's it and then we can run this test open item method you see it prints the sql ambient statement i made car items said that quantity usually here and let me check the database here the car items table right will id1 here and you can see the quantity updated to 10 for that idea made it to three perfect right that's how to update an existing item and lastly let's write the last test method for removing the character from the database it should be very simple let's remove item because we are removing the client based on id so we just call report delete my id here and 2d cut item with id1 that's it in this test method and you can see the sql statement for delete here delete from the current items table and when we check the database you see the item good id one has been removed from the database and you see another example of many too many entities between order and protocol and you can see we used an intermediate anti-class other details here to break the one the many too many units between the order and product into two one too many alien ships between the first one is between order and other details and the second one is between product and other details so this design is for implementing the order a feature and this is a database design as you can see here the intermediate table all the details here it has a primary tid regional column quantity unit price subtotal besides the front keys to the other table and product table here and this is another example of many too many entities between question and customer in order to implementing the question voting feature here a question can be awarded by one or more customers and a customer can vote one or more questions so here we can create a new intermediate class question vote to represent the many too many didn't see between the question and customer and uh it breaks down the many too many and between question and customer into two one-to-many receipts between question and question four and between customer and question board and this is a database design you see in the internet table we have the primary key the additional column and front keys to the question and customer table here okay so that you have learned the concept design implementation of many too many entities and ships in a java project based on report spring data jpa hybrid framework time leaf boot chart i hope you will find this video as a good reference to any manager on many too many entities and ships in your projects please subscribe to my channel like comment and share this video thank you
Info
Channel: Code Java
Views: 23,642
Rating: undefined out of 5
Keywords: Java, Spring Boot, many to many relationship, java many to many, spring many to many relationship, spring many to many example, spring many to many with extra column, spring many to many cascade, spring data many to many, spring boot many to many, spring boot many to many example, spring boot many to many with extra column, spring boot many to many with additional column
Id: AbGxZmfWulU
Channel Id: undefined
Length: 110min 14sec (6614 seconds)
Published: Tue Dec 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.