2

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

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

3 回答 3

0

问题是 Hibernate 找不到字段 userId 的设置器。您已经这样定义它:

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

它应该是:

public void setUserId(String userId) { 
    this.userId = userId;
} 
于 2014-07-04T09:01:54.680 回答
0

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

userId;

但在您的查询中,您有

user_id

你需要使用userIdnot user_id

异常清楚地表明您提供了错误的参数。

于 2018-03-25T10:50:24.943 回答
-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();
于 2013-09-08T06:30:47.060 回答