0

I am facing problem when using hibernate named parameters in spring MVC

Session session = sessionFactory.openSession();
    Query q = session.createQuery("from Users u where u.username = :name and u.password = :pass");
    q.setParameter("name", username);
    q.setParameter("pass", password);
    Users user = (Users) q.uniqueResult();

The output when using log4j is and printing user

2017-01-20 22:34:17 TRACE BasicBinder:83 - binding parameter [1] as [VARCHAR] - "test"
2017-01-20 22:34:17 TRACE BasicBinder:83 - binding parameter [2] as [VARCHAR] - "test"

user =  null

And when I use query like:

"from Users u where u.username = 'test' and u.password = 'test'"

I get the user values.

What am I doing wrong here?

4
  • Did you enable show_sql to see the query that was getting fired? Commented Jan 20, 2017 at 17:17
  • Yes: it is :select users0_.id as id0_, users0_.password as password0_, users0_.username as username0_ from Users users0_ where users0_.username=? and users0_.password=? Commented Jan 20, 2017 at 17:20
  • Can you change setParameter to setString? Maybe the issue is with recognizing the type of username and password objects. Are these objects strings? Commented Jan 20, 2017 at 17:28
  • Did that ,same result :( Commented Jan 20, 2017 at 17:42

1 Answer 1

1

Judging by the hibernate log for basic-binder. Your variable username and password contain two double quotes(at the start and end).

Look at the variables in a debugger or try hardcoding the values in the setParameter methods.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah , I noticed as few minutes ago . Silly me :P. But thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.