-1

I have been trying to write a hibernate query and have been able to generate the hibernate query through the server, but the parameters in the parameterized query are not getting binded correctly i.e. if 23 is to be binded to parameter 1, but it's getting binded to paramter 3.

Any idea why this would be happening? What should be the points of errors that I should check to get to the root cause of this issue?

EDIT (Why the query is not here?)

The hibernate query is a complex one and it is not possible for me to publish it here. That's why my question is a general one. I just wanted to know, if someone else has also faced this issue, in which hibernate throws no errors and the query is generated successfully, but it's binding incorrect data to incorrect parameter.

I will try to give a rough idea through an example --> e.g you write:

criteria.createAlias("D.A", "a", CriteriaSpecification.LEFT_JOIN, Restrictions.eq("a.delIndc", false));
criteria.createAlias("A.B", "ab", CriteriaSpecification.LEFT_JOIN,
                            Restrictions.sqlRestriction("sqlQuery"));
criteria.createAlias("ab.c", "abc", CriteriaSpecification.LEFT_JOIN, Restrictions.eq("c.dataEntry", "Star Wars"));
criteria.createAlias("abc.f", "f", CriteriaSpecification.LEFT_JOIN, Restrictions.eq("f.dataExit", "Star Trek"));

The query is getting generated successfully in the server (no error thrown), but while doing the parameter binding:

2017-12-09 12:09:21,396 TRACE [org.hibernate.jdbc.AbstractBatcher] (pool-14-thread-2) preparing statement
2017-12-09 12:09:21,396 TRACE [org.hibernate.type.BooleanType] (pool-14-thread-2) binding 'false' to parameter: 1
2017-12-09 12:09:21,396 TRACE [org.hibernate.type.BooleanType] (pool-14-thread-2) binding 'Star Trek' to parameter: 2
2017-12-09 12:09:21,396 TRACE [org.hibernate.type.BooleanType] (pool-14-thread-2) binding 'Star Wars' to parameter: 3

So, instead of binding 'Star Wars' to parameter 2, it's getting bind incorrectly to parameter 3.

So, in a nutshell, I want to know, why this might be happening? What are the checks I need to do to get to the root cause of this? And also if you faced this issue, how you found the issue and how you resolved it?

Hope this clarifies my query further. Please help and let me know, if you need any more info in this regard.

Ayush Kumar
  • 45
  • 1
  • 10

2 Answers2

0

You can use 2 ways to bind the parameters in Hibernate - positional parameters, named parameters.

say you have query like:

select name from Emp where id=? and age=?

Here param at 0 position will represent id and param at 1 position will represent age. Here param order is important.

select name from Emp where id=:ID and age=:AGE

Here you can set the params using names ID and AGE, in this case order is not important as you are referring to params using names rather than using their positions.

Amir Azizkhani
  • 1,662
  • 17
  • 30
Har Krishan
  • 273
  • 1
  • 11
0

Provide your code and the values you want to pass in the query.There can be a possibility that you might have modified the name of the property in the pojo/model class

  • I have already checked this. I have not modified any pojo/model class. Anyhow, if such thing would have happened, hibernate would have thrown an error, but the query generation is successful. – Ayush Kumar Dec 11 '17 at 13:47