我需要帮助,我收到了上述异常。我哪里错了?在从类到表的映射中,我使用了以下内容:
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;
}
}
答案 0 :(得分:0)
问题是Hibernate找不到字段userId的setter。你已经这样定义了:
public void setUser_id(String userId) {
this.userId = userId;
}
应该是:
public void setUserId(String userId) {
this.userId = userId;
}
答案 1 :(得分:0)
Hibernate通过变量名映射数据库。所以你有;
userId;
但在你的查询中你有
user_id
您需要使用userId
而不是user_id
。
异常明确表示你提供了错误的参数。
答案 2 :(得分:-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();