hibernate 异常提示:找不到命名参数[userId]

crcmnpdw  于 2022-12-27  发布在  其他
关注(0)|答案(3)|浏览(143)

我需要帮助,我得到了前面提到的异常。我哪里出错了?在从类到表的Map中,我使用了以下代码:

private String userId;
private String password;

下面是我编写查询的类。

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;
    }
}

实体

@Entity
@Table(name = "Login")
public class Login implements Serializable {
    private static final long serialVersionUID = 2L;
    private String userId;
    private String password;

    @Id
    @Column(name = "user_id")
    public String getUserId() {
        return userId;
    }

    public void setUser_id(String userId) {
        this.userId = userId;
    }

    @Column(name = "password")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
cclgggtu

cclgggtu1#

问题是Hibernate找不到字段userId的setter,您的定义如下:

public void setUser_id(String userId) { 
    this.userId = userId;
}

它应该是:

public void setUserId(String userId) { 
    this.userId = userId;
}
3bygqnnd

3bygqnnd2#

Hibernate通过变量名Map数据库。

userId;

但在你的查询中

user_id

您需要使用userId,而不是user_id
异常明确指出您提供了错误的参数。

zbq4xfa0

zbq4xfa03#

检查是否为排印错误

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();

loginId = (String) session.createQuery("select user_id from com.project.model.Login where user_id=:userId and password=:password") .**setParameter("user_id",userId)**.setParameter("password", password).list().toString();

相关问题