本文介绍了org.hibernate.QueryParameterException:找不到指定参数[userId]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助,我得到了上述例外。我哪里错了?在从类到表的映射中,我使用了以下内容:

  private String userId; 
私人字符串密码;

以下是我编写查询的类。

  public class LoginManager继承HibernateUtil {
private String loginId;

public String checkCredentials(String userId,String password){

Session session = HibernateUtil.getSessionFactory()。getCurrentSession();
session.beginTransaction();

try {
loginId =(String)session.createQuery(从com.project.model.Login中选择user_id,其中user_id =:userId和password =:password)
.setParameter(userId,userId)
.setParameter(password,password)
.list()。toString();
} catch(HibernateException e){
e.printStackTrace();
session.getTransaction()。rollback();
}
session.getTransaction()。commit();
返回loginId;



$解析方案

Hibernate根据变量名称映射数据库。所以你有;

  userId; 

但在您的查询中您有

  user_id 

您需要使用 userId 不是 user_id



异常情况明显表明您提供了错误的参数。


I need help, I am getting the aforementioned exception. Where am I going wrong? In the mapping from class to table, I have used the following:

private String userId;
private String password;

Below is the class where I write my query.

public class LoginManager extends HibernateUtil {
    private String loginId;

    public String checkCredentials(String userId, String password) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        try {
          loginId = (String) session.createQuery("select user_id from com.project.model.Login where user_id=:userId and password=:password")
                                   .setParameter("userId",userId)
                                   .setParameter("password", password)
                                   .list().toString();
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        session.getTransaction().commit();
        return loginId;
    }
}
解决方案

Hibernate map your database by your variable name. So you have;

userId;

but in your query you have

user_id

You need to use userId not user_id.

And exception clearly says you provided the wrong parameter.

这篇关于org.hibernate.QueryParameterException:找不到指定参数[userId]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 06:49
查看更多