In Java objects associations are unidirectional. Which means if product has a many to one association with category, it does not imply that category also has an association with product. On the contrary, in our DBMS, if you have a foreign key in one table, you can traverse in both the direction. In order to have this bi directional Association here, we have to go to the category Java, and then create a variable here that represents all the products that belong to a single category. So when a single category object is obtained, for example, beverages, I would like to get all the products corresponding to this. For this, let's come here and then create a variable called list of product and then call it us products.
Make sure that you import list from Java dot util and then add a corresponding getter setters. You can move the cursor on these products and then Create getter and setter for products. Or alternatively select this, go to source menu, generate getters and setters. Automatically it is selected. And I will say that add after the last one here, I say, okay, it's been added. But again, if you do not map this with any at the rate column or anything, hibernate automatically assumes that products is going to be a new column in category.
But it fails because this is not a scalar type again. And we know that this is not going to be a new column anyway in the category because we already have a column corresponding to this association in the products table. So we want to use the same thing. All we have to do is to add an annotation here called one to many. And then we say here, join column with the name of category underscore ID. Now it is important to note that the join column here does not mean that the giant column is going to be in the category dot java, this column is going to be looked in the products table.
And if it doesn't exist, it's going to create a new one. Alternatively, we can skip this and then say that the product already has a many to one Association. And we want to use the same join information for this association. Also, for that, you may have to say your math to by equals to the name of the member that has the many to one Association. So if we go to the product dot java there, you will see that the many to one association is on a variable called category. So this is the name that we have to give here as map device.
So I'm going to save this now we have one to many Association. Before we write a program to check if this is working. Let's go to our dB. I already have added some data into my products table. I also have added few values to categories table also with eight different categories. Now I can write my program to fetch one category and then get all the products corresponding to the same.
So to do so, let's go back to our code. Create a new program here. So I'm going to call the package name as seventh one here. Make it as mean, add the main function as well, fullscreen this and then remove the to do here and then paste the snippet that we have been using so far. import the appropriate packages, I don't need the begin transaction and commit. So let's get a category first see one equals two session dot get category dot class.
And then the ID is that a one which corresponds to beverages, import the category and declare that c one. Now I have the C one. I can print the details of C one by typing category equals to C one dot get category name. can say category description is equals to C one dot get description will also say here, products in this category. And then I want to loop through and print the names of the products. For that I will use a for loop as a product p in C one dot get products, which is our list that we have created and mapped.
Let's import the product also. And then I'm going to type your P dot get product name probably along with the price of the product p dot get unit price. Before we run this let's also go to hibernate util dot get session factory. change this to false so that we don't see an SQL statements because it will be very complex output. I'm going to run this code right now and you can see that it should say beverages has around 12 different products. So as you can see that this is beverages And you've got the description and then these are the different products in that particular category.
So retrieving one category has retrieved other corresponding products also. But if I go to the program and then close the session after retrieving the category here, at this point in time, hibernate has brought the category corresponding to this ID. I have no problem in printing these details over here. But when I start accessing the products in that category, products have not been retrieved yet because by default, one to many is lazy fetch. Now hibernate tries to go back to the DB, but DB connection has been closed because connections are represented using session. This is where you get an exception saying that lazy initialization exception so I'm going to run this fullscreen the console.
You will see that we are able to get the beverages name and description but when I tried to access the products it says Lazy initialization exception. The solution to this is you go to the category dot java and then variable defined is one too many, you can always say here, fetch equals to eager. I save this go back to the program run this, I should now see that all the products have been returned without any problem. However, this is not recommended, as it might pollute the JVM with lots and lots of objects.