Hibernate: user is not mapped
程序员文章站
2022-04-28 16:37:58
...
报错截取:
org.hibernate.hql.internal.ast.QuerySyntaxException: user is not mapped [from user where name=:name and password=:password]
org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96)
org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
代码、配置段:
(1).applicationContext.xml
<!-- mappingResources用来列出全部映射文件 -->
<property name="annotatedClasses">
<list>
<!-- 以下用来列出所有的PO类 -->
<value>edu.qtech.myclouds.domain.User</value>
</list>
</property>
<!-- 自动扫描注解方式配置的hibernate类文件 -->
<!-- <property name="packagesToScan"> -->
<!-- <list> -->
<!-- <value>edu.qtech.myclouds.domain.</value> -->
<!-- </list> -->
<!-- </property> -->
(2).User.java
@Entity
@Table(name="user")
public class User {
private int id;
private String name;
private String email;
private String password;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
...
}
(3).UserDaoImpl
public User queryUser(String name,String password) {
// TODO Auto-generated method stub
String hql="from user where name=:name and password=:password";
Query query=sessionFactory.getCurrentSession().createQuery(hql);
query.setParameter("name", name);
query.setParameter("password", password);
List<User> list = query.list();
if(list.size()>0){
return list.get(0);
}else{
return null;
}
}
原因:
hql语句写错了。user应该写成User。因为hql操作的是类、实例、属性,而不再是
sql 语句的表名。
推荐阅读
-
Hibernate使用中防止SQL注入的几种方案
-
JSP 中Hibernate实现映射枚举类型
-
PHP call_user_func和call_user_func_array函数的简单理解与应用分析
-
Spring 整合 Hibernate 时启用二级缓存实例详解
-
JSP开发中hibernate框架的常用检索方式总结
-
在Django的session中使用User对象的方法
-
解决MyEclipse出现the user operation is waiting的问题
-
SSH整合中 hibernate托管给Spring得到SessionFactory
-
linux mysql忘记密码的多种解决或Access denied for user 'root'@'localhost'
-
浅谈hibernate之映射文件VS映射注解