Hibernate query language also allows us to pass parameters so that we can use them in our queries. To demonstrate the same, let's comment out this function call, and then write a new function called called Get Products by price range. And then we'll supply here something like 50.0 to 200.0. Since that function does not exist, Eclipse is giving me an error. And we can ask Eclipse to create such a function for us. I press Command one, so that we get the quick fix.
It says create a method called Get Products by price range, I press enter, Eclipse has created a new function, and I can use the tab to jump there and select the appropriate option. I select the package here, wide function is fine. It says Double D here I'm going to call this as min and then max over here. Now that we have this function, let's create a HQ Which looks like from product way unit price between I can type here something like min price and colon max price. As you can see that we are retrieving the data from the product class this time and the product classes map to products table effectively the SQL generated quite is the products table, but we use the class name here, the class has a field called unit price. Remember the column name is unit underscore price.
So, this is actually the field in the product Java. We now check whether the price is between this placeholder called min price and Max price. These are the parameters I was talking about earlier, which have to be substituted with the values of min and max received as function arguments here. First, we have to say qR y equals to session dot create query and then We're going to supply the argument as HQ L, as well as the data type that we want is the list of products, I'm going to say here product class. This returns me a query object, let's create a query of product type. But before we can say execute and get the result list, where to pass on the values for this, we can do that by typing your way dot set parameter, and then supply the parameter name, which is min underscore price, the value is min in our case, and then likewise, I'm going to type query dot set parameter of Max underscore price, the value is nothing but max which we have received over here.
Now that the parameters have been passed, we can now say, list equals to Q or V dot get the result list, which gives me a list of products This time, I can say for product p in the list. We say this out We can display p dot get product name, plus its price which is P dot get unit price, save this around the same and we should be able to get between 50 and $200. All the products. If you would like to order them based on the price you can use the order by class and then say here unit price in descending, save this and run the same. We should now see that the result is ordered in the reverse order of the price.