As you might have observed that the session provides operations based on primary keys. If I want to query my database table with the different queries, like for example, if I want to get the products by price range or something else, then I cannot use session directly. Of course session represents a DB connection. Without the session, we will not be able to retrieve any data at all. But in order for queries that involves multiple objects, we use something called hybrid query language. For example, if I want to get all the categories in my database table, we can use this hedge QL.
So to demonstrate the same, I'm going to take up a new program here, I call this as a 08 and then call this as main class. I forgot to add the main function, I'm going to add the main function by typing main control space enter. This time I'll create a session object as a static variable because I want to write multiple functions to test my PL SQL options for that we need to declare this outside. So I'm going to type your static session session. It's not initialized yet. And this is where I'm going to say factory equals to hibernate util dot get session factory, create the variable by pressing Command one, or control one on Windows.
And then I'm going to say here, session equals to factory dot open session. somewhere we have to close this I'm going to say session dot close. Make sure that we import the session from hibernate. Accidentally it came from h2. So I'm going to delete this press Command Shift o and make sure to choose from our hibernate session. Now let's say we want to get the list of all the categories we have salmon write a function here called static void, get all categories and my query would look something like This, we say here, select C from category C. As you may see that this looks very similar to that of SQL or Structured Query Language.
But this is not structured query language because we do not query the tables here. What you see here is actually a class called category, which is our own category class. You can also take the fully qualified class name along with a package, which we don't need to do. So here it looks like as if we created a variable called C of type category. And we are simply selecting the entire category object without giving any rare condition which means that every possible category object will now be retrieved from the DB. Alternatively, if you are not going to use this variable called C for anything else, you can remove this from here and then simply say from category in order for me to execute this SQL query, we first need to create a query variable by typing session dot create query.
And then we specify what is the SQL, which is this. And then the result type happens to be category class. We haven't created this variable cure why so it's giving me an error, I press Command one, or control one on Windows. And you can see that we get a query type, which can give me a result of category objects. All I have to do now is to type pure white dot, get a result list, which is going to return me a list of category objects, I can assign that to a variable called list, press Command one or Ctrl one to get that automatically created. And now I have a list of all category objects.
If I want, I can also say a session close here and then iterate over this, but I'm doing the session closed in the main function to print the names of all the categories, we can simply look through this Type categories t in list and then type your C dot get category name. Let us also call this function from the main here so that we can test this. I save this and run the program and I should see the list of all the categories in our database table.