org.hibernate.QueryParameterException:找不到命名参数[userId]

问题描述 投票:2回答:3

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

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;
    }
}
hibernate exception query-parameters
3个回答
0
投票

问题是Hibernate找不到字段userId的setter。您已将其定义为:

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

它应该是:

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

0
投票

Hibernate通过变量名映射数据库。所以你有了;

userId;

但在你的查询中你有

user_id

你需要使用userId而不是user_id

并且明确说明您提供了错误的参数。


-2
投票

检查是否是拼写错误

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();
© www.soinside.com 2019 - 2024. All rights reserved.