#13 Hibernate Tutorial | Mapping Relations Practical

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so welcome back aliens this is of inventive compass for gardening and in this video we'll talk about hibernates relations but this will be a practical in the last session we have talked about the theory operate so for that watch because what I have done here is I have created two classes one is laptop in laptop I have our ID as L ID and then we have L name and for this we have get in and setup then we have student where you have you see we have okay so we have this student class here and then in our last class what we have done is we have created this laptop object and this student object and I want to store both the objects into database and of course you have to make sure that you have am period both the classes here which is student and laptop and then you can simply add it can simply save the gate vanity database you can see we have saved laptop and save the student now what what what's something organized everything is perfect here let's run this so if you London of course there will be a table for laptop and there will be a table for students as you can see I'm not using any specific name for this or table they will use student and laptops table as I mean only so you can see we have an ID tell name and then we have mass name and roll number now if I'm going to go to the mask Y and you can see you cannot see so I have this neon as my data base and you can see we have two tables here which is student and change show table we do have two tables your laptop and student and if I say select star from students will be getting d t you can spot a student which is 150 Nerine and if I say laptop we can see the Koch 1 to the 1 and del again van waiting for strengthen laptop because in the theory we have talked about these two classes again you can continue the same example for alien class as well which we which we are doing from last few videos okay so what what's new here so what I want is there should be a link between a students on laptop because every student will have a laptop right what I will do here is I will say which lets create object of laptop here I will name it laptop as laptop thing and then for this property we have to use the identity of course so we have to say right leg with the soles which is here and we select getters and setters so we'll select this part and we click on OK ok so you can see regard it is go for laptop as well now so student class has a property which is laptop so whatever I mean see we have a number we have name we have mark and we also have laptops which you have a lots of property right but the last term is not on normal variable it's an object so it will go to the laptop class and see ok in lots of clouds we have L ID as the primary key so in students table will be having L ID as the current key right let's try let's see or before that we have to change in app as well we have to also set the laptop user ID so we say s dot set laptop and we'll say laptop laptop itself so you can see we have laptop here in fact the necklace by missing this thing in fact if I if I want to change this student it should change the all the variable C right I've seen that in some of the ideas I believe but it when you type if you select this if you say if you type student all this reference should also change but doesn't know how to get that if we know let me know in the comment section which ID he supposed that ok let's continue so what I'm doing is I am taking this laptop student as well and I put on this code okay we got an error because because yes so what what's wrong here is it is not able to define the type of laptop here so whenever you create objects inside student you have to specify what is the relationship between this laptop and a student for that you have to use a notation which is called as one-to-one because one students will have one laptop now let's fly in from this code you can see it worked so once twenty one half one is a score that is your one two so that's your one-to-one relationship now if I say show table you can see because laptop and strength this is this this is our old table Cystic north laughs turbine student and if I say the next class on laptop it will be only laptop entry because that don't know about the student but student know the body table it shouldn't know about the desktop right because in student class we have updated with the laptop object that's one two one two one but let's see how to implement one-to-many here for one-to-many water rituals let's say one student has a laptop so of course you cannot use normal laptop here you have to say list of laptops what's the list and then let's create object here itself we say new add a list okay let's import the package for both we'll say Java dot util okay so we have imported that okay it's not getting imported for this of watch okay so since I'm using replaced my a project to one point six and that quite is not supporting this diamond here you have to specify that this feature of not mentioning was introduced in one point seven two of course you have to mention here because my JDK as to one point six okay so instead of using one two one I should be using one two may now right and for this you have to input the package let's do that now what will change is we have to change the get a theta as well so instead of saying good laptop with a return the list of laptop and here will mention these of laptop as well okay now in app in shop setting the laptop you have a say F dot get laptop dot add you have to add this laptop there right you have to add this into additives so but if it changes what we have done as in the student class we have mentioned about one-to-many and we have created the list of laptops not one laptop you have these soft laptops and in this app what you are doing is you are saying get laptop which will give you the added ease and then you can say add I don't think we work it will run on this code okay you can see the Gordon error is because it is not everybody to delete the table okay this is important because sometimes because of dependencies is not able to delete that the problem here is it is not able to digit the laptops tables because our student table because student table has a primary key of laptop right so you have to delete this part they first have to visit the laptop officer visited student here so that it will read a new table and it is it cannot delete because okay so we already got so you can see regard the third table does it do it one by one you know this is what you have to do is anyway you create when you will run you have to make sure that you did it all the extra stuff so we'll so then delete the table and let's drop drop and drop much once let's go to a clip and say is one this should not be an issue now and you can see record the output but the change here is if you see the output you can see we got laptop table that make sense you got swing table that makes sense why do we have shown in laptop because we cannot do mapping in shooting table now because one student will have multiple laptop you cannot do that into into student that's why it has created a mapping table that you have mapping between student and a laptop and that's where the name is student laptop right and if I say finished star there if I say select star arm Sybil's good let's say select star from student laptop and if I say father's query you can see the clear because rule number one got lotsa 1 to the 1 and if I say student table they don't know about the laptop and if you subscribe sub table they don't know about they stood it right but that's awesome right so that's how you are one-to-many work but well now I think laptops should know about difference as well because if laptop knows about the student and one laptop I mean one half will belong to one so and only right so if you can create this student reference in laptop as student and for that of course we need cadence better let's do that with a right click close and we'll select cater and setter but this time it will be for students will click on ok because I was trained there but this student should know mapping their doing suppose for one student we have multiple laptops lies when student we have student has multiple laptop that's what is one too many because one student will have much of a laptop but in case of laptop it is multiple lots of belong to one student so we have to use many to many to one now why am i - one is because multiple our table will belong to one student so with this the mapping will be done inside laptop table we don't need extra table to have that okay but before running this thing again the best thing is to drop all the table so that you know it will not get any girl running table students and will that will drop laptop as well now if I run this code okay so you can see it has critic laptop laptop table that makes sense because it has la villita as student role imagine a feedback now this laptop table knows about this student this laptop belongs to watch students right and then we got Flint which does not know about the laptop because they have a mapping table but when you when you have this entry in the laptop table why you need a mapping table against the frames of them laptop think that he is possible to map student think that he is responsible to map we have to mention we have to mention here H student you don't have to map it because this will be mapped by and then you have to mention that it is mapped by this variable here with the student so he don't have to do it I will simply specify student so that for this time your student class will not map it but again before running this I have to draw all the tables let's drop this let's draw this one and let's plot this one as well oh it is not able to delete the student table because there was a laptop table and let's tell it that is it working now oh it says unknown table near luck students bathtub is it the table me and watch what's wrong there let's run this thing up once again and can see that this time we are not getting on new table it's because the mapping is done by laptop so show us how do not have to care about that right so that's how we use mate one so that meant one-to-many many-to-one there is a spot let us say this laptop has list of students here so we don't have one student we have I mean we don't have one string so multiple students will use multiple laptops like you know let's say if you have 10 laptop enough energy up in a college lab a computer lab and then any student can use any laptop right that's fine too many too many right let's import the package for this let's say control shift oh and we'll say this is such let's say new array list and this time was mentioned student ok with input the package for this as well so where's the analogous right and then interesting made what made to one we have to say may too many raise the input package for that as well and here we'll return the list and while spending also we have to set the list not one swimbot list of students right so that's done we have created this laptop variant where mapping is done with the student but here in app where we change something the laptop should know about the student as well how do we do that we'll say laptop dot set such student then we have to say set student or not the essentially should we get student get student dot add and you have to mention student but that should be done afterwards somewhere here because once you have created the student Dino sequence you can specify that by so now student has added laptop and laptop is adding student so we should work now you see we have this of students we still this talk laptop but if you make sure that you like may to money and here also you have to mention many time is not one too many unlikely fact if I not if I'm not I think this mapped by nature London let's import the package for this okay what I'm doing is in list we are mentioning many to many to one laptop belong can belong to multiple students and one showing you can have multiple laptop before running this of course we have to do that stuff let drop which table anyway don't we don't have that table laughs and let's drop these two tables okay now if I say show tables you can see we have only agent there is no footprint of student and laptop let's run this let's see what happens if I miss code now you can see it works but how many tables we have we have lots of table that makes mention table have make sense lot faster and also makes sense but why we have slid laptop when mapping is done by laptop why shouldn't touch do that it's because lots of things okay he's responsible to map it shouldn't think that is responsible to wrap it let specify the student here you don't have to worry about that this is done by the laptop so why you are wrapping it so let's say let's again do the same thing map by student okay let's drop all the tables because if I say show tables you can see we've got four extra tables there let's drop all the tables with the drop table laptop under school student okay let's drop this one let's drop this one let's drop this one and all the clips are dropped now if I on this thing okay and you can see the number of tables they got laptop regard student and they got last option and now we don't have anything called as student laptop and if I go to my skewer just to see how many screens regards let's remove let's say this is laptop student let's party pony for laptop we got laptop and three five equally for students we constituent and three and if I say laptop strange we got the mapping as well again one laptop can have multiple students and multiple are talking have watch function that's of it well right so that is one-to-one one-to-many many-to-one and may too many so if you have any queries let me know in the comment section and that's it I hope you liked this video click on the like button and do subscribe for for the video
Info
Channel: Telusko
Views: 152,832
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, hinernate mapping relations, mapping, navin reddy, how to do hibernate mapping, hibernate one to one mapping, telusko learnings hibernate, java tutorial for beginners, hibernate one to one mapping example foreign key, hibernate one to one mapping using xml, hibernate mapping example using annotations, hibernate mapping multiple classes to one table, hibernate mappings using annotations, hibernate mapping annotation, hibernate mapping tutorial
Id: 8mUPvdDGoLQ
Channel Id: undefined
Length: 15min 36sec (936 seconds)
Published: Thu Jan 19 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.