SSH框架中的hibernate提示 “user is not mapped”问题
程序员文章站
2022-04-28 16:38:40
...
在整合SSH框架时,我的dao层报错“user is not mapped”,报错的方法是checkLogin()。
public User checkLogin(String s_name){
User user = new User();
String sql = "SELECT * from user where s_name = :s_name and s_flag = 'Y'";
SQLQuery query = (SQLQuery) getCurrentSession().createSQLQuery(sql).setParameter("s_name",s_name);
query.addEntity(User.class);
List<User> users = query.list();
if (users.size() != 0){
user = users.get(0);
System.out.println(user.toString());
return user;
}else{
return null;
}
}
错误描述:上边出错的地方在我写的HQL语句上
解决方案:在HQL语句中的表名应该是你ORM映射的名称,而不是你的数据库中的表的名称;
所以将HQL中的user表名换成User,再次运行问题就不出现了。
改后:
String sql = "SELECT * from User where s_name = :s_name and s_flag = 'Y'";
我的实体类:
@Data
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "s_id", unique = true, nullable = false)
private String s_id; //用户ID
private String s_name; //用户名
private String s_passwd; //密码
private String s_phone; //联系方式
private String s_address; //地址
private String s_email; //邮箱
private String s_flag; //当前状态
public String getS_id() {
return s_id;
}
public void setS_id(String s_id) {
this.s_id = s_id;
}
public String getS_name() {
return s_name;
}
public void setS_name(String s_name) {
this.s_name = s_name;
}
public String getS_passwd() {
return s_passwd;
}
public void setS_passwd(String s_passwd) {
this.s_passwd = s_passwd;
}
public String getS_flag() {
return s_flag;
}
public void setS_flag(String s_flag) {
this.s_flag = s_flag;
}
public String getS_phone() {
return s_phone;
}
public void setS_phone(String s_phone) {
this.s_phone = s_phone;
}
public String getS_address() {
return s_address;
}
public void setS_address(String s_address) {
this.s_address = s_address;
}
public String getS_email() {
return s_email;
}
public void setS_email(String s_email) {
this.s_email = s_email;
}
}