在使用hibernate时,出现了异常:users is not mapped:
Exception in thread "main" org.springframework.orm.hibernate3.HibernateQueryException: users is not mapped. [from users]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: users is not mapped. [from users]
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:656)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:912)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:904)
at com.lwframework.user.dao.impl.UserDAOImpl.findAll(UserDAOImpl.java:31)
at test.Test.main(Test.java:18)
Caused by: org.hibernate.hql.ast.QuerySyntaxException: users is not mapped. [from users]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:157)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:265)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3049)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2938)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:218)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:158)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:109)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:75)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:54)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1583)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:914)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:1)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
... 5 more
Caused by: users is not mapped.
... 27 more
数据库中有一个USERS表,用Users类进行映射,User.hbm.xml检查了好几遍,确定无错,代码中查询语句如下:
String hql="from users";
运行时,出现以上错误。
明明已经映射了Users了,为什么还说没有映射到users呢?对照着错误检查了好几遍,突发奇想:users没映射到,那Users呢?在学sql语句时就知道了sql语句中对大小写是不敏感的,虽然这是hql语句,但差别也不会大到哪去吧?不过想归想,我想是试了下,结果居然然成功了,不再报错了!
总结:HQL与SQL语法大体相同,但本质含义却不同:
SQL是对数据库中 的表进行操作,操作的内容是表和字段,对大小写不敏感
HQL是对类及其属性进行操作,操作的内容是表所映射的类及表中字段所映射的属性,HQL可以对SQL中的关键字(如from,select等)大小写不敏感,但是对类名及属性名大小写敏感,必须区分大小写。