#12 Hibernate Tutorial | Mapping Relations Theory

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so welcome back aliens this is I've been ready from those car landings and in this video we'll talk about hibernate relations now if you are coming from the data base background you know what I'm talking about right so we have different relations to work with right so we have one to one we have one to many we have made too many right so one cable can have its record Xindi so let's imagine we have employees and we have department table so normally what we do is so one I mean one department will have multiple employees so that means they have one department multiple employees which means one-to-many and if you look at look at from the employees perspective it is many employees work for same department that is for this many to one right then we have laptops so I own one laptop so which means one to one and one laptop can belong to any one person right so there is one to one but but let's say if you go to a library when you have buy multiple you have lots of books so the same book can be I mean so multiple people can use the same book I mean they can you know in rotation basis or one person can take multiple books because multiple people can work with multiple books that is called as made of many right so you know all this stuff comes from data from the database world right so that's not the whole topic here though the whole thing is how can you implement those things in hyper night because that we can do in SQL right and we are expert in that we can create all those primary key foreign key stuff in SQL how about hibernate let's try to do that here so you can see we have two examples here the first one is we have a class laptop which has this two records we have Li D and L name and then we have we have student class here which has roll number name and marks and these are two different entities right and you can see I am using a notation which is identity and identity which means we'll be getting two supplied tables all together one will be laptop and one will be student right so let's imagine this is those tables so we have laptop table where we have Li D and we have L name then we have student table where we have roll number name and marks which is your columns here and again we have roll number as the primary key and then we have your lig as your primary case of this column and this column are your primary keys right but let's say now I want to create a relationship so a laptop so let's say student will have a laptop object and can we do that and of course we can do that right so imagine this is the class and again we have some Gators inside us here the way we normally do so on both the end we have getters and setters now let's say in this student class if I am creating object of laptop because every student will have a laptop right so what we'll do is we'll say private and that will be laptop and then we'll be having laptop okay by any mean if you are thinking that we have a different font altogether so this thing is written by someone else and I am writing this this stuff here so you will find it a different points that so now what I am doing is I am creating one more object here which is laptop right so we have a laptop object here which is which is inside your student class now how will it impact your table so now since we have one more column there what we'll be doing again you guessed it right again we have to create a fallen key and the primary condition ship here so this class will have one more I mean this table will have one more column here and that will be your L ID right so we will be having a laptop here let's say let's imagine we have laptop one and then laptop name is Dell and then we have roll number one let us say this is one zero one rule number one name is let's say Naveen and then I am saying marks equal to 50 and then n ID which is your one zero one which specifies that we have a relationship between this laptop and a student where this is your foreign key from your laptop table right so so yes that's how you can relate to that Naveen Reddy has it has a laptop which is Dell laptop right so that's how you can create one-to-one but will it work directly not exactly to achieve this sum well here just about your object declaration you have to mention and which is called as at one-to-one okay this this Oh is capital here so you have to use an annotation called as one to one so one student will have only one laptop so student table will get affected and you have to mention that Alya their simple right so when you have one object one laptop object in student table that's how you do it now let's say I don't want to use one laptop so in fact either student or as a professional I have multiple laptops let's say I have one del 1 HP laptop and one MacBook so if you have three laptops here I cannot use one object here right I have to use a list of laptops so what we do now is instead of having a normal laptop entry here let me go for list of laptops again we have okay that's your paint marks but let's say if I create private and we say list of laptop and then we'll say laptops again when you change all these things you do have to change the getters and setters again we'll see that in the in the practicals how do we do that but time in what you are focusing is those photoshop so when you have one student with multiple laptops and for this scenario you have to use an annotation called as one-to-many because one student will have multiple laptops right so you have to use annotation which is one too many again without any space so it is act one-to-many that means one student will have many laptops but in this scenario how will you update your tables because say this will not work because one student will have multiple laptops let's say we have laptop 1:02 here which is HP and 1120 buys I mean has both the laptops how can you how can you mention that here you cannot say 1 0 2 comma 1 0 I mean 1 0 1 comma 0 2 right and you can't even have two records here because zone number is a primary key so you cannot say one naveen 51 0 2 because none of us are prime so it will give you error so in this situation what we normally do is let's not change this table so this table will be independent so storing table will have its own records and laptop will have its own record there will be one more table which will be called as student under schoo laptop so there will be one more table for restaurant as for laptop in this table you will be having two columns the first column would be student or in fact roll number because that's your primary key so it will be having the old number and the second column would be L ID now the entries would be it is 1 then 1 0 1 then 1 1 0 2 let's say if you have one more showing here which is one number 2 and his name is let's say Aarthi and then the marks is let's say 56 and then I am using 1 0 3 here which is Mac Book which is let's say Mac Book okay and let's say Aarthi has only one laptop here which is mac book so what we'll do is we'll say 2 & 1 0 3 so you can see we have a table here which is used for the mapping so this is a different table called as student laptop which has a which has all the relationship entries right because we cannot do that here but hold on we have a solution for that right instead of creating a new table what if you can just change your laptop table I mean something like this what if some guys here I don't have a place to do that let's say I am just creating a column column you it should be at the end but let's say since we don't have a place I will created here can I create a whole number column here so this is your primary key okay can I create order number column here and every locked laptop will be defined so this this laptop belongs to so number one this laptop belongs to remove on this laptop belong so number two simple white so instead of having the entry in the another another table we can have the entry in laptop itself but hold on how it will work because student table knows that it has laptop entry your laptop class don't know that it is rated to student how will this column will be created so you have to mention in your laptop class that every laptop belongs to a student so for that I will be using private student and then we'll say let's say stood again if you don't do this if you don't write this the the output would be laptop table without this column student table and then student underscore laptop table but if you do this if you write private student I mean private student stood now this class will know that we have a column or we have a field here of which you have to connect a column right but again we are fetching the whole number right so this should know that we are going for a relationship and the way you can do that is with the help of act many-to-one annotation so when we use add may to an annotation which means that this this laptop class is responsible to create a column from the student sorry from the student table and that will be roll number right so in this shouldn't you have to mention one too many you have to mention that and in laptop you have to mention may two one now if you if you do this if you run this code the expected output is laptop table with this column student table with this field there will not be any extra table right but hold on it will not work you will be getting extra table as well even if you don't want that your shouldn't shouldn't we'll say here you asked me to create one-to-many it is my job to create that table laptop will say you have mentioned many to one it is my job to add this column why both are doing that again ego problem maybe so you have to mention to your student class that the mapping is done by laptop so you do not have to do it now how can you mention that you can mention here mapped by property so you can say mapped by and in double quotes you have to mention the name of the field which is stood so if you right studio you are mentioning to your laptop I mean you are measuring to your student table or shouldn't class that I am doing one too many that's perfect but the mapping is done by laptop don't do it by yourself that means if you do this it will not create an extra table it will go for in two tables laptop and student and you'll be having the entry at the end of the stand the end of this table which is roll number okay so that's how you use one-to-many a one-to-one one-to-many many-to-one but how about many to many how we can do that let's say so we were having one student will have multiple laptops and then laptop will have one student but what if you have many to many relationship so one student can use multiple laptops and multiple laptop can belong to one student that's right so multiple laptop can be used by multiple students so we have many to many relationship here to achieve that instead of using stood or student wait to create a list of student so we say list of student and I say this is stood since obvious joint will say stood now this time we have laptops which has multiple students and we have student which has multiple laptops so we cannot use one-to-many Omega one here we need to use many too many and here as well you have to mention many too many now again if you like made to many and made to many without this map by both the classes or both the tables will be having their subordinate table so if you don't write mapped by what it will do is laptop will create laptop table students will create student table but laptop will also create laptop underscore student table and student will create student underscore laptop table so in total they are getting four tables we don't want that right the mapping should be done with only one table now this will not be a this will not be applicable because one laptop can have multiple students so we cannot do that so we need to use a mapping table which is which will be called as let us say laptop underscore student right and you can achieve that with the help of mapped by property on attribute so that's how you use one-to-many many-to-one May to mind all the stuff so in the next video we'll be talking about the practical implementation of it so I hope you like this video how exactly it works let me know in the comment section if you if you have some issues with this and yeah that's it thank you so much for watching and do subscribe for further videos
Info
Channel: Telusko
Views: 208,320
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, hql, mapping in hibernate, hibernate mapping tutorial, hibernate, hibernate mapping, hibernate mapping multiple classes to one table, hibernate mappings using annotations, hibernate tutorial for beginners, hibernate mapping tutorial by durgasoft, hibernate mapping tutorial with examples, hibernate mapping tutorial with annotations, association mapping in hibernate, collection mapping in hibernate, one to many mapping in hibernate
Id: VLlDaIcb3jE
Channel Id: undefined
Length: 13min 14sec (794 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.