日期:2014-05-19  浏览次数:20679 次

没招了,Hibernate 的 not mapped 问题
错误提示:
org.hibernate.hql.ast.QuerySyntaxException: User is not mapped [select count(*) from User u where u.setUsername = :username]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:111)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:327)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3441)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3325)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:733)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:584)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:244)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:256)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:187)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)

错误所在行
HQL语句 :session.createQuery("select count(*) from User u where u.username = :username")
.setString("username", u.getUsername());

实体类和表名都是 User
配置文件 :<mapping class="com.bjsxt.registration.model.User"/>
折腾一天了,就是不知道哪错了,求指点。。。

------解决方案--------------------
你配置文件写错了吧,Hibernate配置文件第一层标签是<hibernate-mapping>,你写成了mapping,先看下是不是这个错误!
------解决方案--------------------
select count(*) from User u where u.setUsername = :username]
里面的u.setUsername属性必须是你User bean中友这个属性,不知道这个回答是不是你想要的