Hibernate JPA Cascade Type (ALL & PERSIST) Using Spring Boot Application

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is satish here today we will discuss about the hibernate jpa cascade type uh we will be mostly focusing on the cascade type all and cascade type assist in this talk and we'll be making a basic difference between what is cascade type all and cascade type versus using a springboard application so just to give you an overview as it says like when we are doing a cascade type all in a relationship entity so the we prop we propagate all the operations from the parent table to the child entity so when we do cascade type all it would propagate everything from the parent to the child table but in cascade type persist it will only propagate the persist operation from the parent child parent to the child entity so this is the basic uh like the statement says for the parent uh to child propagation using the persist and all so let's jump back to a springboard application and see this demo yeah so i have a springboard application where in the properties file i mentioned my jdbc driver here so i'm connecting to my local database using my local password and username and the port in which the application will be running on triple seven zero and the uh yeah and we are also printing the sql queries in the logs so let's just go and look into the applica author table as well so in the author of entity we have uh it basically exchanged the base class so in the base class we have two properties here like the created at and updated at and parallely we are updating and setting the created date or updated according to the update on insert scenarios yeah in the author table we all have the primary id the name and we have one-to-many relationship from the author has a one-to-many relationship with the book we have this relationship mentioned here and in the book entity we sorry in the book entity we have it also exchanged the base class it has created and updated by and it has a id and a title that's a simple book entity we have and so we in the controller i have some apis here like the add author update author and delete find author by id and find all the authors so these are some of the apis so yeah so the most important is the cascade time here so add in this uh example the cascade type is all so we'll be looking at some of the apis and observe the pattern what happens in the cascade type wall and then we will change it to cascade type persist and see the difference so let me run this application before running let me delete the database yeah so the database is deleted let's just run this application yeah the application is running so the tables will be created along with the database so let's just directly jump back to the tables and so we have two tables the author and the book table and let's just run one of the apis so i'll be using adding adding an author here so yeah so this is the author add properties we have set like the body here we have the author name and the books with the author resources associated and let's just run this so as you can see the author is created along with the two books let's just verify in the database as well so we have the author created the created and updated date are same here and in the book also we have two books created with the foreign key of the author okay let's just jump and update the author so for this i have a put api pass the author id here skip the id yes one let's change the author name one two three this is okay i'm only changing the title here i'm not updating the created so it will be null update this here so this will be the gasket type all yeah so let's just look at the database and see what is the difference so in the name i have updated it so yeah the name is updated along with the updated date is also updated here and let's just uh refresh this table as well yeah so here we have the updated it updated so i have i didn't pass the credit so it is null and the book name is also updated so this is like uh i from the parent uh the the request is sent back to the child table as well so we have used the cascade type all the means the propagation happening from the parent to child as well so now let's delete the author and see what is the behavior so if i go and delete the author with id1 so it says 200 okay let's go and see the uh author author is deleted in the book uh both the books are deleted so this is the behavior with the cascade type all so we will repeat the same thing with the cascade type persist so let's just change the code okay now let me delete the database let's just rerun let me prepare the request as well first as a post id we don't need to pass let's just change the name back yeah so this looks okay see the application the application is running let's see the tables so yeah in both the tables we don't have any entries let's go and hit the api so this was this is with the persist example yeah so as i can see i get a response let's see verify that in the table so we have the first author is created along with two books yes now let's go and update the author so it's supposed and let's say i give the name one to three ideas one sorry ids one the book name i change it let's go and keep the id to change the book name yeah so this is the update scenario let's run this you see we have updated the put the author and the book no sorry let's see in the database and observe what happens in this scenario so yeah the book uh sorry the author is updated we have the uh name change and the updated is also updated let's see in the book table so let me refresh so as you can see there is no change in the book table so no change in the title and no change in the updated this is because uh we are using the persist as a cascade type so if i jump back here we are using a persistent type so it only propagates the information which are related to persist means adding something into the tables means if something is added to the author table that information will be transferred back to the book table as well which is our child label so this scenario will only like the parent book will only be happening in case of a persist in other cases so this is a case where we are actually updating the uh document which is present in the table which is a merge operation so in this scenario the merge will not be propagated to the child table so that's why we don't have a book updated in the child table or in in the database as well so this is with the book and book like the update api so if i go and delete the delete the author as well so in this scenario as well according to the uh cascade type but uh delete will not be like will not be means propagated to the child table which is book so the delete the book will not be deleted in this scenario as well so let's observe the uh queries as well so let me clear this once and let's yeah let's hit this api and see okay so we have the status as 200 so you can see the table first the author the author is deleted and in second case uh just observe the foreign keys here so you can see the foreign keys have been removed but there is no change in the uh no change in any of the properties like the updated is also not updated so now let's go and see the queries which basically run behind so here we have we have selected the author first then we have updated uh updated the book so in this scenario we updated the foreign key to the null and then we deleted the author so there is no deletion of the book is happening in this scenario this is because of the persist cascade time so that's pretty much about like i want to discuss about the cascade eyeball and cascade i persist using an example so yeah i hope it will be helpful to someone so thank you for watching
Info
Channel: satish kumar
Views: 273
Rating: undefined out of 5
Keywords: #hibernate, #jpa, #springboot, #cascade, #persist
Id: MjI_1-RVs2k
Channel Id: undefined
Length: 10min 23sec (623 seconds)
Published: Sun Aug 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.