Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not all named parameters have been set hibernate in createSQLQuery

I am getting the error of not all named parameters have been set. Below is my code.

my SqlQuery which is running fine at mysql prompt, You can refer schema in the question SQL Query

SELECT  t.*
FROM    (
    SELECT  @lim := 2,
            @cg := ''
    ) vars,
    (select * from Table1 order by product,amount, make)  t
WHERE   CASE WHEN @cg <> product THEN @r := @lim ELSE 1 END > 0
    AND (@r := @r - 1) >= 0
    AND (@cg := product) IS NOT NULL
ORDER BY
    product,amount, make

my java code

try {
             context.dbl.startTransaction();
             Session session = context.dbl.getSession();

             //String sqlQuery = "from com.infibeam.inventoryservice.dbObjects.PopularBrandDO";
             String sqlQuery = "SELECT  t.* ";
             sqlQuery=sqlQuery + "FROM    (";
             sqlQuery=sqlQuery + "SELECT  @lim := 2,";
             sqlQuery=sqlQuery + "@cg := ''";
             sqlQuery=sqlQuery + ") vars, ";
             sqlQuery=sqlQuery + "(select * from Table1 order by product,amount, make) t";
             sqlQuery=sqlQuery + " WHERE   CASE WHEN @cg <> product THEN @r := @lim ELSE 1 END > 0";
             sqlQuery=sqlQuery + " AND (@r := @r - 1) >= 0 ";
             sqlQuery=sqlQuery + " AND (@cg := product) IS NOT NULL ";
             sqlQuery=sqlQuery + " ORDER BY product,amount, make";
             //Query query = session.createQuery(sqlQuery);
             SQLQuery query = session.createSQLQuery(sqlQuery);
             listItems = query.list();


            }catch(RuntimeException e) {
                e.printStackTrace();
            }

Below is the exception i am getting

org.hibernate.QueryException: Not all named parameters have been set: [] [SELECT  t.* FROM    (SELECT  @lim := 2,@cg := '') vars, (select * from Table1 order by product,amount, make) t WHERE   CASE WHEN @cg <> product THEN @r := @lim ELSE 1 END > 0 AND (@r := @r - 1) >= 0  AND (@cg := product) IS NOT NULL  ORDER BY product,amount, make]
    at org.hibernate.impl.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:291)
    at org.hibernate.impl.SQLQueryImpl.verifyParameters(SQLQueryImpl.java:199)
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:143)
    at com.infibeam.weaverbird.helper.PopularBrandFacetHelper.bootstrap(PopularBrandFacetHelper.java:48)

Thanks in advance...

like image 942
mahesh Avatar asked Sep 27 '12 06:09

mahesh


People also ask

Why are all named parameters not set in hibernate?

Not all named parameters have been set. Named parameter not bound. Although the error messages may differ between vanilla Hibernate and its JPA implementation, the root cause is the same. In this tutorial, we'll take a look at what causes these errors and how to avoid them. Along the way, we'll demonstrate how to use named parameters ...

Why is my Hibernate Query Not working?

If so, Hibernate is expecting a parameter and you're not setting it. or similar. I can only assume your value has a : in it which does not signify a parameter so you should build this as a string and set that as the parameter. Show activity on this post. You should not concatenate String to build your query but instead use parameters.

How to bypass named parameter query in a dynamic query?

You can bypass the named parameter query by puttong if condition when it is dynamic. So you make it as a query string and then append your named parameter by putting a condition.

What is an example of a named parameter in a query?

Let's look at an example of a query that uses a named parameter: In this example, we have one named parameter, indicated by the :eventTitle placeholder.


1 Answers

The problem is the assignments with :=, which are by the way no standard SQL.

In SQL after a : always a parameter is expected, like in where value = :param and :param has the be set as a parameter then. Now hibernate is scanning the select and find colons where no set parameters follow.

Solution: Redesign your selection using hibernate standards.

You can use two different HQL queries.

First: Select all product: select distinct product from Table1

Second: For each product you do from Table1 where product = :prod, :prod you set as a parameter with the actual product, and with setMaxResults(2) you can limit the number of rows as you need.

Now it is many selects and not a single one, but nevertheless they might be faster than the single query (the single query is complicated and risks an inefficient search strategy in the database). And a big advantage, now it is purely HQL and so your program is portable to different databases.

like image 148
Johanna Avatar answered Nov 15 '22 18:11

Johanna