Spring Boot One To Many Tutorial with Thymeleaf, Bootstrap and MySQL Database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the video spring port one to many tutorial with me nam amin from code java net through this video you will learn how to design one too many entity deviant ship in java programming with a spring framework and in this video i call the entity on the one side is a parent and the entities on the many side as a children and you will learn how to code crash functions on one too many dealership in a springboard web application with three different cases the first guy is cloud on many to one side with existing pattern and the second case is cloud on one to many sides with existing children and the third case is clutter on one too many sign with new children and this picture will help you understand the first guy with many the one you see between product and category that means the category contains one or more products and a product belongs to only one category and when creating a new product which is a new children we can choose an existing pattern which is the category from a droplet list analysis and later in this video you will learn how to code this form and the second case is an one-to-many density between brand and category plan contains one or more categories and category belongs to only one brand and when creating a new brand which is a new parent we can choose some existing children which are some categories from the list like this and later in this video you will know to implement this functionality and this is a third case with an one-to-many relationship between product and product details that means a product contains one or more details and details belongs to only one product and when creating a new product which is a new pattern here we are also creating new children which are some product details here and you will also learn how to implement this kind of one-to-many vmc later in this video so through this video you will learn how to develop a java web application that performs cloud operations on some one-to-many new union ships using the following technologies spring boot web application spring data jp and hybrid for the data access layer time leaf as a template engine html5 and bootstrap on the client side and my sql database and through this video i'm using the flowing software programs in java development kit jdk sprint 2 3 id sds mysql committee server and master workbench you can use the latest versions of these suppliers on the computer this is screen 2 3 i'm going to create a new swing boot starter project so go to menu of my new spring starter project and enter the project name as for example inventory with the default options for project type is maven version 8 inventory app and description they want too many input and click next and you can see the spring boot version is 2.4.1 and for the dependencies we need to choose spring web time leaf spring data jpi master zbc driver spring boot adapt to for automatic restart and this has a basic dependencies okay you can choose the dimension in the in this section here and click finish okay you see the project got created here as inventory app project here and you can see this sees a basic directory structure of a springboard start project this is a main class that runs as a supreme court application as you can see here resource directory is required we will put the html5 configuration file and this is a maven project file from xml and you can see the dependencies for spin mode starter data jpi time leaf web lab tools rest qr code java and by default it includes the screen boot starter test here and you can see that there are files for the dependencies here under the maven dependency section here you can see screen push start that data api and you can see i have net here and because spring data jpi uses as the implementation you can see lot of azure files automatically downloaded by maven next we need to configure data source information for the database so open the springboard application properties are here and specify some properties for the data source information spring note data source dot url is the jdbc url jdbc colon minus qr code and low cost is the host name of the mysql database server 3306 is a port number and slash the database schema name is inventory because i'm using an instant numbers clear the community server on the same computer so the host name is localhost if you connect your remote mysql database server it should be ib arrest or host name other than localhost and this image workbench and let me create a new database schema here the name is the inventory here that is the name here apply apply finished and you can see the newly created database schema inventory here and sales default schema and you can see this time no tables at all and back to the project we need to configure the username and password to connect to the database username here here i'm using the username root and password and for using a spring data jpi with a harmony framework uh we should use this property spring.jpi not have net ddr auto here equal grid so hybrid work we automatically create the tables in the database when the application is running so for the first time we specify the value credit for this and property and i want to show sql statement in the output so i specify this property in spring.jpa.show sql equal to here and for hamlet i want to format the sql statements jpi dot properties dot hybnet dot format and it's called sql it was true this property we tell have different work to format the sql statements printed in the control view and then i need to use a specific dialect for my sql database so i specify this property spring.jpg properties dot timeline dialect and this is a heimlich hybrid dialect class or dot timeline dot mysql5 he know bb dialect you can see the correct class name of a direct class in java file of hybnet here i'm good call and look in the package direct here and you can see uh the various classes for database dialects here and you can see masked qr5 you know db dialect class here okay so that's for the configuration of the data source next let's create the home page for the application so first we need to create a new spring mvc controller class here app controller this is a spring mvc controller so we need to use this annotation controller here and the first handler method is for the home page so that we use a red mapping annotation to handle http and that request with the empty string is for the the application context root here public chain in view home base and exist handler method we just written a logical view name for the homepage which is usually the index based okay and then in the termness directory here we need to create the corresponding html file html here name it as index.html finished and here we go the code html code and change the base title to inventory home base and create a heading one in the best welcome to inventory that's it and now we are ready to run our stream application you can see the boot task bar here in spring 2 3 id here and you can choose the application and click the start button here to start the application okay you see screen boot application is being started and you can see some login information in the controller output here and you can see by default the application is running on an embedded token server listening on port number 8080 with the context part is empty here and we can use a browser like chrome to test the application so i open the application.this url http localhost part number 8080 context path is empty and you can see the welcome message welcome to inventory app here that means our springboard application is up and running perfectly and it displays the content of the homepage as it is here and because we will use bootstrap for the user interface so we need to modify the maven project here to include some dependencies for using good job here so create a new dependency section here with the group id is [Music] oh dot website a website is a kind of technology that enables using web resources in a java application from java files and artifact id is the boot chat and the version of boot chat is 3 by sorry 4.3.1 and then we need to specify another dependency for the website allocator so we can copy the rope id is same and artifact id is the website dash locator dash card here this dimension allows us to specify the you know bootstrap cls file in the html5 locator okay save the changes then you will see here we see the jar files for websites good job here you see and under this draft is the resource for bootstrap you can see css files here and then we can update the home page in the head section here to include a reference to bootstrap and css file link url equal style sheet type is in text css strap part to the part of the main cns file and here we use the time diff and xml ns th equal http www.10 here to use templates syntax in this html file and i recommend you to use dh for every url that is generated dynamically here and then we specify the part to chop main cns file which is the website slash bootshop slash css trust much not mean.cls and then we can use the css class names in the html here for example for dip section here we can specify the class container here and a deep section for the heading one here okay that's it and we can refresh the application homepage here refresh and view the resource to make sure that bootcamp.cos is loaded click here and key and you can see an error here so i think i need to explicitly restart the application here for the changes to take effect and refresh the project and start the application again okay and refresh the hook page and you can see the style you got changed for the heading one here view press us click the link to the bootstrap css file here and you can see the content of the cs right here that means bootstrap is uploaded successfully next we are going to code the grad functions on many to one difference between product and category the category contains one or more products and my product belongs to only one category so we need to create two entity classes product and category that map to the corresponding tables in the database table product and table category but first we need to update the whole page of the application to include the hype links there for listing categories listing products creating category creating product okay so i create a new deep section here yeah and i have link here this categories and the url is using timely for the er url here slash categories and i want to center the text in the container so i use the class dash center from butcher here refresh and you can see the have linked list categories here i use the class heading 2 for the hub link heading 2 s2 and you see is a habling list categories here there's a handler method it has not been created yet so we see the white label error base and then the hype link to create a new category career category and the url is category slash new it should be in the separate dip section so it will be in the separate line you see list categories then create category and we need to use the spacing class in a boot chat b2 wedding 2 to add embedding space between the deep sections and you can see it looks better right okay and we do the same for a listing have link to list products and create a product contacts is a url here list products yeah and the same similar for the hub link that allows the user to create a new product products slash new career product okay and refresh and you can see we have four hybrid links list categories create category list products and click product in the home base here and now let's create the first entity class category that maps to the corresponding table category in the database the table has two columns id name so the entity class will have two corresponding fields id name so come back to the project and create a new java package here for the category cut memory and refresh and create a new entity class here category with two fused id and name there the better and setter method slash generator stands at the cycle there right okay and we need to use some annotations ntt yeah from the package zavax not persistent here and because the class name is same as the table name so we don't need to use the table annotation and for the id field we need to use the id annotation to indicate that these field maps to the primary column in the database and the related value with strategy is the generation type identity to tell hybrid framework that the values of the id field will be directed by the database and for the column name we need to use annotation column and we specify the length maximum length of the name is the 45 character and it is not it cannot be null so nullable equal force and the name of a category is unique so we specify the ltb we need equal to okay that's it for the category entity class and now we need to create a category repository interface here category repository i think it's better to stop the application because i'm going to write a lot of code regular make a lot of changes to the code and we have this interface extends the [Music] api repository interface extend jpi repository interface the jpi depositor interface defines common message for data access you know the entity type is category type of the id field is integer you can see the jpa repository interface here define some methods file save here and then we can use those methods without the need to write implementation code because address tab spring data api we automatically generate implementation for the repository interface here okay and now we you need to write any test another test source directory here create a new class category repository test here we create a new test class for data jpi so we use the annotation data jpi test yeah and i want to run this test class against the real database so i need to use this annotation auto configure test database newly blessed equal none because by default data jpa test class we used an in-memory database instead of the real memory so if you want to use the real database you need to use this annotation with the best property set to none and i also want every changes will be committed to the database so i specify robot equal force here and then i can have a reference to the category repository here no report and has spring framework auto write an instant category repository into this test class and the test method first test method is just create a new category proceed a new category object into the database probably test career category and i create a new category object with a name so i examine the contractor here so the united contractor using fuels let's make all the red and we copy we need to have three contractors and one for the no argument contractor yeah no a woman and the second is for the id and third is the for the name okay and yeah now i can call repo save new category you category object with the name for example electronics yeah and this method you know returns the category object so size or return value accept category here so i can use the search statement let's set number defaults bring put includes the search library so we can use the fluent api for system research that accept category yeah id is greater than zero so you can see using a certain uh assassin api here you can see there's a assertion statement looks more fun than the traditional you need they need a search statement okay then we are ready to run this test method and in the database you can see we have no table at all yeah very fast you see no table at all oh sorry the table category has been already created here because when i make changes to the code and the application has restarted and when we started it we created a table you can see the sql statement here drop table it exists category and sql statement grid table category here because in the application properties file we set this hibernate ddr auto equal credit so each time the application restarts it we recruit the tables in the database so you see the table category we got created here now let me select uh enter this table and you can see the structure to column id name here and you can see 45 exactly the size that we specified in the entity class like 45 here and you can see if we chew yeah not know to here okay that means that we need to change uh the ddr auto here to update here so we want to recreate the table and now let me run the test method here and run as jb test you see the test has passed successfully with the ruling check mark here you can see in the control view it prints the insert statement here insert into category table and let's never define the database select rows from this table and you can see a new row one inserted with a name electronics yeah perfect right next i'm going to write code that displays a list of categories here so when i click the i blink list categories here all categories in that way will be listed the application is not running okay so i create a new controller class here category controller gc uh spring rbc controller so we need to use the annotation controller and here in the controller class i can have a reference to the category repository category repository to report here and has premium work automatically inject an instance of category repository with the auto wire annotation here and the handler method is for this url for listing categories here public exchange list categories and it returns the logical view name to the html paste that will display the list of categories categories and here i can call the report method file or method here that returns a list of category objects and assign it to a variable list categories and i need to use the model object from framework to store the list categories into the model model and attribute list categories here that's it for the controller handler method that we display a list of categories for this url here and then we need to call the view best categories here so we can reuse the code of the whole best index so i copy and paste here and change the file name to categories here and i change the html code here to list categories the title of the best and the heading of the best is the tell the list delete this code from the home page okay now we can start our springboot application to test the listing categories in feature and i use a dip section to for the table that shows the categories okay refresh you can see the home page here and click the link list categories and you can see the category list page with the heading category list here let me right code to display the category i want to use the table tray pro here happy with some columns id and name of the category and then the table body t body and i use the timeline block and with dhh to i thread over elements in a collection category in the collection list categories if you see is a list categories is set in the model yeah this categories here okay and like for each category object in the collection i create a new table row to display the content of the category the first column is the id category.handy and the second column is the category name here we use a name from the field names in the entity class id name here we need to specify a table row here okay basically for the category list best you know we can refresh and you can see the one category electronics appear here but it doesn't look nice right so we can modify apply some css classes from bootcamp yeah to make as a user interface look looks nicer okay so for the table i use this glass glass equal table table water table heading class table and dark okay and refresh and you can see the table category looks better right with id1 for the category name electronics here next i'm going to record for implementing the great category function here when the user clicks the click category here form we appear allowing the user to enter the information for category okay so i need to update the category controller here write a new handler method to handle the url categories class new here you got mapping for http event request public change so category new form with a model object and i put a new empty category onto the model model and attribute name is category new category and return the logical view name to the category from bash and theory underscore file and let me create category from here copy and paste the home place can tell you the score from here is a title create category create new category the heading one of the best is create new category okay now let's click create cuddle link here you can see adding great new category and uh let me write code for the form from yeah and the action of the form we use the timely diet action equal cut every slash save in gh object refer to object in the model category the object is set in the handler method category here and then the method of the form is http post and then i use a dip section for the form and label label is category name so in the category form we have only one text field that is the category name input type equal text the edge field equal to the name field of the category object here and use a cns class from bootcamp from control require apply some cns from the label which is the [Music] column form label in the class for the deep section here is the class equal form group in draw and you can see the form appears here category name the text view here and i center this form on screen so i use this style max the width is about 600 pixel and i center the form on screen so i use a buzzing zero or two and you can see here okay and let me update the cns for the form sorry this will be correct and refresh column small 4 and for the detection for the input field is the column small 8 yeah and refresh and you can see the form is looking nice now you can see there's some spacing here amazon 3 here and have a submit button here button type equal submit save and cs class is btn btn primary you know center as a button text center with reading three okay and you can see the create new category forms here with a text view and a submit button click save and you can see it requires to fill out this view because we use html5 input with the required property here and next last royal handle method for this url for saving a category in the category controller class here this time we use the first mapping this url change save category and we can specify category object as a parameter for the method and timely we populate the fields on the form and create a new category object here okay and then we just call report save category and finally we return redirect to the category list best here redirect okay and we are ready to test the grid category function okay so i enter category name as [Music] spot click save and you can see it returns to the category list best with new category appear spot here perfect right and we can verify in the database you can see new category spot all inserted here and let me create another one from the home page create category category name auto mobile here click save and you can see automobile what created here let me create another one food and refresh here click save and you can see it's the new category food and beverage here next let's create the entity class product to implement the one too many different ship between category and product and as you can see in the product table we have four columns id name varies and category id is a front key to the category table so i create a new java package here product and create a new entity class product yeah this entity class maps to the corresponding table in the database entity and the first field is for the idea of product and then product name and price and we have a reference to the category category for the id we need to use notation here then the value an entity and for the field name that maps to the corresponding column in the database we specify the length is about 128 characters nullable is false and also the name of our product is unique so we need equal to here and for the priced field we don't need to use any annotation and for the reference to a category here because a product belongs to only one category so here we need to use the annotation manage one many product many products belong to one category we need two one here and specifies as i column which is a foreign key in the product table category underscore id now column name equal and then they are made the letter and set of methods select all and they have it okay and you see and save the changes you'll see the application restarted and you can see the sql statement printed in the control here create table product here now let me check the database refresh the database inventory tables and you can see the new table product code created here with four columns id name price and category id here exactly what we specified in the entity class and you'll see here category id is a foreign key front key here very convenient using helmet and ddl auto as you can see here we just specify dd or auto eq update so whenever we create a new ltd class and when the application restarted it we create a table automatically okay so far so good and then we need to create a new repository interface for this product entity class product repository and have this interface extends the api repository product integer asset and next i will implement the code to create a new product when a user clicks the link creator product here go back to the project and i create a new spring rpc controller class first product controller to handle request for the product module you see a controller and we need to have a reference to the product and repository yeah and the first analog method is to display the great new product base that might be the url is here products is last new roughly change show new product for and return a logical view name to the product for product underscore and we need to have a model object here and put a new product onto the model product new product object here okay and then create the great new platform we can reuse the corrupt category form so copy and paste here then modify the title heading create new product create new product action of the form is slash products slash save yeah the object of the form is the product and the first text field is a product name product name and the view is name because in the entity class product we have the field name price okay the next view is for the product price so we can copy and paste this section product price mapped to the view name varies type of the input is number with step is 0.1 to allow inputting float number okay and in the homepage here click the create have link great project i bring anyone choose a create new platform with two fields clock name and project price here and because product uh belongs to a category so we need to display a list of categories here on the form so we need to modify the controller here to add a list of category objects so we need to have a reference to category repository category repository here category so this name is product report what why here in the handle method that shows the new problem first we need to add a list of category category report file or here and this return list collection list categories and add this list categories onto the model so in the product new form will be we can display a select double list for categories here in the product form so copy and paste this deep section for new field yeah category and in timely we can use the select queue here in the edge view that map maps to the category view of the product entity class category here okay and then class from control require and for the elements in this select we need to use timelift th block th colon h which category cut in the list categories list categories stand from the controller list categories here and then we create a new option element here with the text is the category name the text equal to name and dha value value of the option equal to dot id id of the category that's it and refresh the create new product page and you can see the drop down list displays a list of categories here and when creating a new product we can choose the category to which the product belongs okay and then code the handler method that this is a new project object and into the database post mapping action products slash safe which is action of the product form here you see products select safe here and we declare product object here to map with the drug object in the form and we return a react to the home branch temporarily because the list product has not been implemented yet here we just call product report save product object so you can see it is very simple and easy using bootcamp sorry using spring data jpi with timelife okay save the change and we are ready to test creating a new product before that now let me check the product table here you see currently no rows in this table and now let me create a new product for the category on electronics yeah let's help soon 789 and click save and you can see it directs to the homepage and let me check the database and you can see new product uh inserted into the product table here with category id is a one and you can see only one is for the category electronics here electronics yeah perfect right so you see using spring data api with time lift is very easy to implement the a new uh product of [Music] an existing button an existing pattern is the category which can be choose from the drop-down list here you see now let me create another product for the category automobile here for example tesla model 3 price 21 000 click save refresh the product table here and you can see new product got inserted here perfect right and you can see the category id is three which is the category id of the category automobile tree here next i'm going to write code to implant list products feature where the user clicks the link list the products in the homepage here so i made the project controller class for adding a new handler method here might be and the url is yeah exactly the url here in the comparison here roughly exchange list products you need to have a reference to the spring rbc model object and return the logic of the name products and here we can use the product repository by method that returns the list collection product objects from the database list products and we put these these products onto the model okay and then we create the product list paste you can reuse the code of this categories so copy and paste and modify products and modify the title of the best is list products product list is handy and for a product we have some columns id name price and category contemporary here we use timeless with forage for each product in the list products and collection product id and product name okay you can click the have link list products you can see name there are two products samsung galaxy and just a model 3 here perfect right and next and columns price and category product.price and product.net we can use nested attribute like this category is an object in the of the product here and in category we have the name here now refresh you can see we have all columns for product name price id name price and category now let me modify the project controller to react to the project yeah after saving a new product okay let me test again create a product here and this time i create a new product in a here running through for example price is 2 7 2 8 9 click save and you can see it returns to the product list test and you can see the new product appears here perfect right so in this product a new form yeah create new product we can create a new product for many to one student ship create a new children with existing parents chosen from modern analysts here now i currently product in food and well-rest here for example coca-cola price is 0.5 click save and you can see coca cola appears here perfect right next yeah i'm going to implement the edit update product function that allows the user to update a specific product so i need to update the product list base to include a hype link to allow the user to edit a specific product so in this column we put a habling here ghff edit and the link is like this slash products slash edit and then follow by the blog id plus product dot id and you can see the link edit here click here and we need to implement the corresponding analog method in the controller class in the product controller class all here let's pin for the url button like this id is the id of the product now click stream show any product form and it returns a logical view name product underscore form which is same as the new product form and here we need to use the annotation but variable here to map the value id here in the url id in the integer id which is the id of the product to be edited in the model object here so here we can use a product or it was repository here fly by id method here and it will return optional object so we've got here so we might already get and return the product object here and then we put this product object onto the model product product okay then we can test editing the product here click edit for samsung galaxy and you can see the edit product form appears here we should change the title of the quest here so it is a product from here and we need in edit mode we need to have a hidden input start the value of the product id to be edited here th equal product and here we can use dh if the condition that the product id is no that means it is you displays a new platform and otherwise unless clock id is not no that means edit an existing product okay refresh and you can see the heading of the best edit product here the pesos and you can see the value of the hidden field here really one and we need to give a name to this needed field and name now we use dh field equal to the field id id field of the product object here refresh and you can see we pass us id here and then we need to display the parent category of the product so we need to add a list of category objects from the database we can reuse this code here okay and refresh the edit product paste here you can see the category of product samsung galaxy is electronic here perfect right now go to the product list test and i want to edit the product coca-cola and click edit here and you'll note that the category is food and beverage here click edit and you can see the selected category is chosen automatically here perfect right now i want to edit the product for example auto mobile from test envelope to tree to electronics and click save and you see the category of the product test model 3 yes updated to electronics here perfect right change running shoe to food and beverage save you see upgraded chain marks again so you can see it very simple and easy to implement the edit the function from one too many different ships next i'm going to implement the delete product function that allows the user to remove a specific product from the database so update the product list best here to have a hype link for edit for deleting a product here the url is products slash delete and followed by the product id and you can see the highlight delete yeah i have blink delete here and then we need to image the corresponding handler method in the controller class so we can copy this method here change the url to product slash elite slash followed by the idea the product daily product and in this annual method we simply call product or report delete by id and then we return the direct to the product these best products here very simple right and we are ready to test deleting products here i want to remove the product running shoe here click delete and you see it has been removed now let me check the database and put that and you can see no more rolling shoe here let me delete the project calcula delete and you see it has been actually removed from the database perfect right so i have done coding the grad functions on a many-to-one dealership between product and category creating a new product as a new children with existing parent with category can be chosen from the droplet list here and you can see it is quite simple and easy with spring data jpi and time leaf next you'll learn how to implement cloud operations on not too many different ship between two entities brand and category the brand can contain one or more categories and a category belongs to only one brand and when creating a new brand we can choose one or more categories which are the children here and in the java code we need to create a new entity class brand that maps to the corresponding table plan in the database with two columns id and name and in the category table it will have a front key brand id that refers to the primary key of the brand table here come back to the project and we create a new java package for the brand module here new java package dot brand here and create a new entity class brand brand here entity in the first field is the id integer id and then the name of the brand and then because a brand can contain one or more categories so we need to have a collection of category objects here i use a list collection list and brand sorry list category categories equal new array list and for the id field we need to use the id annotation here and the the value annotation here okay and for the field name here we need to specify the maximum length is 45 characters nullable equal force unique equal to and for the collection list collection of category objects here we need to use the animation annotation one too many one too many because the brand can contain one or many category and we specify the mapped by attribute that is the true name in the category side class select then red yeah okay and in the category entity class there we need to have a reference to a brand private brand here meaning that the category belongs to a brand brand and for this we need to use the annotation many to one and specify the zoid column is the front key grand id in the category table and also generate the negative and setup methods for the new view brand here they are red in the brand here okay save the changes and if we update the tables in the database and you can see is a sql statement that create a new table brand here and alter the table category at the front key brand id here let me verify in the database and refresh the tables here and you can see table brand here with two columns id and name and the table category has a new column new front key brand id here let me enter this table and you can see the front keys in this category table that refers to the brand table here perfect right and next we need to create a new repository interface for brand yeah brand repository and have this interface extends the jpi repository for the entity class is brand type of id field is integer next we are going to write code to implement a great new brand function so let's update the home page to include two new have links for listing brands and for creating new brand so we can copy and paste here list brands url is slash brands in creating your new brands new brand friends slash go to new brand and refresh the home page now you see the new tool have links list friends and create brand here now let the royal handler method to display a great new platform for this url so i need to create a new controller class here run controller using the controller annotation and have a reference to brand repository blend report go to wire and the first analog method is to show the click new form get mapping for the url here public chain shell create new domain form and return the logic of the name of the brand html form which brand underscore form and we need to have a model object from the spring rbc here [Music] attribute new empty brand object here new plan okay save the changes then we need to also create the html5 for the brand form so we copy and reuse the code of the form here copy and paste here lorraine underscore from.html and change the title of the page to create your brand click new brand and it brand and the action of the form is brand slash shape and object is breadth method post id easy id if you observe and the first text field is for the brand name okay and then we need to have a list of categories here so this that should be choose one more categories and in the controller class we need to write a list of categories and set it onto the model so here we need to have a reference to the plan category repository cut every repository category and you have the list no category objects here list categories and put this list categories onto your spring mvc model okay so in the brand new form here we can use a select select element here the view is categories categories is a few name in the plan entity class categories here it is the collection okay galleries okay and for this select yeah few we need to use this attribute multiple equal multiple here to allow the user to choose one or more elements from the list okay that's it and we can test the new brand from great brand we got an arrow here property of uid can not be found on null right this is error okay we already have over here copy paste mistake very common mistake right okay refresh and you'll see a great new brand form here and you can see a list of categories here and here we can use the control key or cp to select multiple categories here you see i select three categories here or you can use zip to select continuous categories use kanjoki hold the ctrl key and click the mouse or to select different categories yeah click save and we got a validation error and we need to write another handler method in this controller class to proceed the brand object on the form into the database so we use first mapping with the url is slash branch slash save uplink string shape vary for a brain object from the form and we simply call brand report save but an object here and return the logical view name for the last redirect to the whole test because the list has not been implemented yet okay and we are ready to test creating a new brand for one too many dealerships with existing children and let me check the database you can see the plan table here select browse you can see now rows and refresh the category level and you can see in the category bubble we have a new column which here front key to the brand table brand id no by default here now let me create a new brand for example samsung is in electronics and click save you can see it directs to the home page and let me check in the database refresh has not been updated you see no values for all the categories here so to fix this issue we can specify the gasket type attribute in the entity class brand for the one too many association here cascade equal to cascade type proceed so only save or update as a pattern we save and update the children so when we are an object brand is created or updated the children will be updated as well okay and we are ready to test again correct brand and numerator name is honda category automobile here click save and that's in the control view let's verify the database okay now see it is still not let me check in the create new brand from the edge few categories let me use this resource of them create new platform here we have select one two three four five for the categories here it seemed to be correct nothing wrong i think i should restart the application because i think when i make change to the category entity class here it has not been admitted for the new field brand so i need to restart the application and test again basically we don't need to specify the cascade type in one too many annotations here okay now that's creating a new brand away and this time i create a new varana for the category spot as it has yeah and click save still not updated let me check the database updated button we have we have three brands here with id name but the category table has not been updated for the brand primary key here okay so let me try to update this brand ntd class remove these attributes and use a joy column here and name equal to the front key in the category table here right on the sky and just create a new brand again and this time i could only bring now in category is the food and beverage click save okay now it works and you can see it prints the statement to update the category for the brand id here so that means that we need to use the one too many enjoy column for the collection here and let me check the database for the brand we have the new brand coke id4 and in the category table and we have the category food and beverage with id updated to 4 here it is working right next i'm going to write code to implement the list brand function and when a user clicks the list brands have link in the homepage here the list of brands will be displayed so i need to update a brand controller to have a new handler method you get mapping slash brands slash sorry just friends public chain list friends and returns a logical view name which is brands you can have the model object from spring mvc and here we use the brand the repository like for method that returns that returns the list of brand objects list brands and put this collection onto the model hit achievement list branch okay enjoy create a new html file for the list of brands so we can copy and modify this file best brands this brand is a title here i think this and for brand we have some columns that are id name and categories of the parent categories one or more categories and brand name okay in the test list friends you can see there's a plan list as it has corner and for displaying the list of categories of the plan we can just specify the property name categories which is a film name in the entity class brand categories here and then then we need to override the two chain method of the category object here so category name will be displayed so there are two string method for the brand name category name yeah let's return the name okay and we can refresh the plan list here and you can see the category of coke is full and perverse and the other brands having the no category because in the database here they are having no values for the brand id next i'm going to write code that allows the user to update edit and update a specific apparent so i need to update the brand quest here to have the edit action so i can copy from the products list based edit action here edit plan so the url is friends it's last edit followed by the brand id here and you see the editor link here and we need to imagine the corresponding handler method in the controller so we can also copy code from the product here the handler method that shows the edit brand for url slash brands and eat id so edit brand form and return the logical view name which is a brand form and here we need to get a brand object from the friend repository based on id file by id id here and it returns an optional object so here we go and put this brand object onto the model here we also need to retrieve a list of categories okay but then here and let me check the brand form okay now we can test the edit brand function here i click edit for the brand as it has here and it and now i can choose the category spot for azidas here click save list brands and you can see the category spot has been updated to the brand name as it does here let me check the database and you can see here spot category updated to the id of the brain as it has here correct right and i modify the controller here to direct to the brand list yeah whenever i make changes to a plan now i edit the honda brand here and i choose electronics and automobile two categories yeah click save and you can see the brand holder is now having two categories electronics and auto mobile here let me verify the database you see the category electronics and automobile assign brand id to here correctly right and i added the samsung brand here for the category electronics here and you can see for this kind of relationship i want too many when i set electronic to samsung it also remove electronic from honda here and let me check the database here you see all the categories are now assigned to its burns here so why you have learned how to implement collateral versions on an one-to-many relationship between two entities a brand and cut theory and when creating a new brand new parent object here you can choose one or more existing children here and you can see with the help of spring data jp and timely the coding is quite simple and easy and next i'm going to show you how to implement cloud operations on an one-to-many liaison between two entities product and product details in which when we creating a new parent object here we are also creating a new children which are some details here and in the java code we need to create a new entity class product details here that maps to the corresponding table product details in the database with four columns id name value and the front key that refers to the primary key of the product table okay come back to the project and we need to create a new entity class and this package protocol new class product details project can has can have one or more details finish ntt and for this entity class we need to specify the table name which product underscore details and we have the fused id name and value and a reference to the parent entity which is the product product product and generate the letters and status for this field here select all and for the id field we need to use the id notation generated value strategy generation type identity and for the column name we need to specify the maximum length is 45 character nullable equal force and the same for the field value and for the product view here we need to use the annotation many two one may need to one because one or more product details belong to one product and specify the zoid column is a front key project in the sky that's it and now on the other side in the product entity class here we need to declare new field which is a collection list collection to the product details here details equal new array list here and for this we need to use the one too many annotation mapped by the product field in the graph details entity class and cascade type is cascade type oh so all the operations on the parent object product will be cascaded to the children product during objects product details ok save the changes and when the erection being restarted time net we create an updated table you can see the sql statement to create the product details table and update them let's create the product details table here let me check the database refresh row and you can see the new table product details here with four columns id name value and product id here perfect right and let's say close from this table and you can see it is empty for now and now let me update the product new form to show the section that allows the user to input product details here in the product in a new form here you can see go to the home page you can see create a product here and we will display three text fields for the product details without the category here so here we can copy and paste this project name field here this label is in details number one and here we need to use the input type equal text and the name is detail name of the input field is detail sorry it's not detailed it should be a detailed name because the detail contains the name and value and the size of this column is and also for the text view for the detail value here detail value okay in refresh and you can see the first detail here name and value and specify the text for the placeholder name and text for the playstyle for value yes and you can see name and value and similarly copy and paste this section for another details the second and the third details the second details number two and the third details number three in the application you may use jquery or javascript to allow the user to add as many details as the user wants okay so here we have three details fields here you see and then we need to update the handler method in the product controller class here is a handler method say product here to read the values of the details name and value here so we need to use an http request object here sd submit request request and we need to read the values manually for the get [Music] parameter values for the fuel detail name detail name which is an array of chain the tail names and similarly for detail values detail value so because on the form we have multiple text fields with the same name detail name and detail value okay and then we need to iterate through h value name here for integer e i equals 0 i less than detail names length i plus plus and then we need to add a product details to the product object here so in the entity class product here we can implement the method that is then public avoid add detail for even the name and value this details is this collection as you can see here and we need to create a couple of contractors so generate contractor using fuse here for name and value on the server product yeah and we'll also need to have an arrogant contractor okay so in the add detail method here we add a new product detail here new product details for name value and the parent product name value and current product is this object okay and then in the controller handler method here we call product and detail detail names element at the i index here and detail values at the index yeah that's it okay save all the changes and we are ready to test the good new product with details okay here now refresh the new form and this time i create a new product under the category electronics my phone 12 for example price one two three four detail number one is the memory value is for example 256 nearby and the detail number two is the operating system is ios 12 and the detail number three is camera as the resolution is the power 128 megapixel yeah and click save okay it returns to the product list and let me check the database for the product details table here and you can see there are three new rows inserted here memory os and camera here and in the and you can see is a front key value id5 for the product id5 which is the product iphone 12 here so first i would write and now i will update the code of the product list paste here to show the details of each product to do so i need to update the product list page here products here and the next column the column next to category is in details here and values is the details collections in the entity class product details here and we need to override the two check method here so the detail will be printed in okay zero to string here for the field name and value and we just return name equal value okay save the changes and refresh the product list page here we got an error here property of use details cannot be it be found object of thai protect sorry i need to generate the letter and set the method for the collection detail so the red contract sorry so there is it [Music] all sorry it in the product product entity class just generate letters and shutters for the details field here okay and refresh and you can see the details information of the product iphone 12 here memory os camera here perfect right next i'm going to update the edit update function here that allows the user to update the details of a specific product here okay so go to the product form here and this section is uh for the case that a new product will be created so here i use the th block with if condition here it the product id is no that means it is going to create a new product and and block here and in case we are editing a product so you see unless the edge unless you see the edge if okay and i use a dh block here next block the here [Music] no need i can use a th for each here dhh for each detail in the product details here product.details i need to have [Music] input type equal hidden hidden field yeah to store the id of the detail the name is in the tail id and the value is the th value equal to detail not id and then i copy this lib section for displaying the label and text field for detail here in edit mode and we need to have a status variable here to display the car number here status not count yeah and we don't need a place number here because it is in edit mode and we specify the value so for the detail name dh value equal to detail dot net and the same for detail value here detail dot value okay that's it and let's try to edit the product iphone 12 here and it and you can see the details of iphone 12 appears here memory os camera here okay and let me and then we also need to update the handler method in the controller class here to read the detailed id from the request like this yeah detail id and then in the for loop here we need to check if detail id is not known and the tail id has a line greater than zero that means the product is in edit mode with some details else if we for the case that the product is new and here we need to code another method in the product class here to update the details public voice set detail integer id it is the id sorry it should be on the class and product details product details here oh let me see okay okay sorry it is in this project detail string name chain value and this details yeah and new product detail with id name and value so we need to have another constructor in this class for other fields um generate contractor using fused id name and value in an edit mode okay so here we can create a new product details with id name and value that's it and in the handler method here we call set detail here id name and value name and value here and id is the integer value of the tail id element at the index i yeah okay and also changes and we are ready to test the edit update uh product with details here and refresh and now i change the detail memory to 512 here by for the memory the name of the details number two is operating system and the name of the details number three is camera resolution and details number three value is 64 mega pixels yeah and click save button hmm we got the details lost let me see i think it's because in the cell detail method here we don't pass the reference of the parent to the product details so here we need to have a reference to the parent which is a product here this okay save the changes and for now let me delete all the products here really delete and create a new one great product here um next one twelve price one two three four electronics name and memory and 128 gigabytes in os i os 12 and detail number three is camera 128 megapixels yeah click save and you can see it's a new product iphone 12 with these details here and click edit now i added the memory from 128 to 256 gigabytes os to operating system ios in 10 camera 2 camera resolution and value to 64 megapixel yeah and click save and you see the details they got updated memory voltage system camera resolution here perfect right and let me check the database here product iphone 12 here you see and product details here and you see we have the details for bulgari sikkia and you can see the details for the production know here of the previous product so this rows here apply finished and let me delete this product okay and verify the database and you can see the details have been deleted as well no more product here so far you have learned how to implement cloud operations on an one too many variancy between two entities product and product details when creating a new parent which is a product here it also creates a new children which are the product details here and you can see with spring data jpa and timelife it is not very difficult what we need is to write some actual code to handle all the details of the children on the phone like this as you have seen okay so that's the complete tutorial about implementing collaborations for one too many different ships with the three most common cases in us remote application with time lift boot chat spring data jpi and my sql database i hope you will find this video as the word reference please subscribe to my channel like comment and share this video thank you
Info
Channel: Code Java
Views: 18,967
Rating: undefined out of 5
Keywords: Java, Spring Boot, one to many, many to one, Spring Boot one to many tutorial, Spring Boot many to one example, Spring Boot one to many example, Spring boot one to many mapping example, spring boot one to many jpa example, spring boot one to many relationship
Id: ctwRpskAeIU
Channel Id: undefined
Length: 135min 27sec (8127 seconds)
Published: Wed Dec 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.