简体   繁体   中英

hibernate query with named parameter giving no result

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM