Employees表的结构:
Employee.Java 代码:
package com.qiuclass.persistent; public class Employee{ private long eid; private String ename; public Employee(){ } public Employee(String ename){ this .ename=ename; } private void setEid( long eid){ this .eid=eid; } private long getEid(){ return eid; } public void setEname(String ename){ this .ename=ename; } public String getEname(){ return ename; } }
Employee.hbm.xml 部分代码:
< class name = "com.qiuclass.persistent.Employee" table = "EMPLOYEES" lazy = "true" select-before-update = "true" > < id name = "eid" column = "EID" type = "long" > < generator class = "increment" /> </ id > < property name = "ename" column = "ENAME" type = "string" /> </ class >
主程序部分代码:
public void createQuery(){ Session session=sessionFactory.openSession(); Transaction tx=session.beginTransaction(); Iterator iemp=session.createQuery("from EMPLOYEES" ).list().iterator(); while (iemp.hasNext()){ Employee emp=(Employee)iemp.next(); System.out.print(emp.getEname()); } tx.commit(); session.close(); }
当代码编译到session.createQuery("from EMPLOYEES").list().iterator(); 就报错: org.hibernate .hql.internal.ast.QuerySyntaxException: EMPLOYEES is not mapped [from EMPLOYEES]
Hibernate.cfg.xml配置文件上的<mapping resource="com/qiuclass/persistent/Employee.hbm.xml"/> 相对应的文件没写错。
开始时候一直在检查EMPLOYEES表各字段属性与Employee.java类属性是否不一致导致,也反复检查Employee.hbm.xml映射文件,都没找到原因。 后来经大牛指点,才知道hibernate.hql是对类进行查询的,EMPLOYEES是表名,Employee是类名,所以要改成 session.createQuery("from Employee").list().iterator();
原来又是概念没搞清楚,折腾了头天
全篇转载:http://blog.csdn.net/qiustion/article/details/47910239