欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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 语句的表名。