hibernate:运行时出现user is not mapped问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_17505335/article/details/51853368

在用hibernate框架,出现如下问题:


出现原因及解决方案:

源码:

String hql = "from user";
Query query = HibernateSessionFactory.getSession().createQuery(hql);
List<User> list = query.list();
for(User g:list){
System.out.println(g);
}

上面的语句出错时因为自己写了HQL的语句,其中我写的语句是:
string hql = "from user";

但是在HQL语句中表名应该是ORM映射的类名,而不是在数据库中的表名。所以解决方法将HQL语句中的user改成对应的类名(User):

String hql = "from User";

再次运行就不出现该错误了。

Hibernate问题:QuerySyntaxError: User is not mapped,帮帮忙吧

05-31

郁闷啊,运行Hibernate应用程序出现了一个怪问题:应用程序在Jbuilder2006中运行正常,但是打包发布到Tomcat5.5后却在登录验证时出现User is not mapped问题(测试后也发现不能映射其他对象),验证代码如下:rnrn //在默认构造函数中已进行Session,Transaction等初始化,Transaction已开始rn //整个应用程序在Jbuilder2006中运行正常,但打包发布在Tomcat5.5中运行到rn //query = s.createQuery(sql);处就报错rn public boolean checkLogin(User user)rn rn tryrn rn String sql = "from User u where u.userName='" +user.getUserName()+"'";rn ps.println(sql) ;rn query = s.createQuery(sql); //报错,产生User is not mapped异常rn User myUser=(User)query.uniqueResult();rn if(myUser!=null)rn rn if (myUser.getUserPwd()!=null) rn return myUser.getUserPwd().equals(user.getUserPwd());rn elsern return user.getUserPwd()==null;rn rn rn catch (Exception e)rn rn System.out.println("select User Error:"+e.getMessage());rn e.printStackTrace();rn rn return false;rn rnrnhibernate.cfg.xml如下:rnrnrnrn rn jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=MyDatarn org.hibernate.dialect.SQLServerDialectrn sarn sarn com.microsoft.jdbc.sqlserver.SQLServerDriverrn truern threadrn rn rn rnrnrnUser.hbm.xml如下:rnrnrnrn rn rn rn rn rn rn rnrnrn异常如下:rnorg.hibernate.hql.ast.QuerySyntaxError: User is not mapped. [from User u where u.userName='admin']rn at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:63)rn at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:196)rn at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)rn at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)rn at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:425)rn at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:880)rn at org.hibernate.impl.SessionImpl.list(SessionImpl.java:830)rn at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)rn at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:603)rn at com.wang.business.BusinessManage.checkLogin(BusinessManage.java:49)rn at webapp.LoginAction.execute(LoginAction.java:22)rn at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)rn at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)rn at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)rn at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)rn at MyFilter.doFilter(MyFilter.java:36)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:731)rn at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)rn at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)rn at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)rn at java.lang.Thread.run(Thread.java:595)rnCaused by: User is not mapped.rn at org.hibernate.hql.ast.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:85)rn at org.hibernate.hql.ast.FromElementFactory.addFromElement(FromElementFactory.java:77)rn at org.hibernate.hql.ast.FromClause.addFromElement(FromClause.java:67)rn at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:217)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:2830)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2719)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:513)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:371)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:201)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:151)rn at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)rn ... 32 morern

User is not mapped

10-28

ERROR ExceptionEncoder:129 - cn.com.gsoft.core.exception.BusinessServletException: User is not mapped. [from User o where o.loginName = ? and o.applicationId = ? and o.deleted <> 1 ]rn at cn.com.gsoft.core.context.ContextFilter.doFilter(ContextFilter.java:84)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:165)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:145)rn at cn.com.gsoft.core.exception.CatchExceptionFilter.doFilter(CatchExceptionFilter.java:63)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:165)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:145)rn at cn.com.gsoft.core.servlet.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:110)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:165)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:145)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)rn at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)rn at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)rn at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:955)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:139)rn at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)rn at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)rn at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:955)rn at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2460)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:133)rn at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)rn at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:119)rn at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)rn at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)rn at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)rn at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:955)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:127)rn at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)rn at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)rn at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:955)rn at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:157)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)rn at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)rn at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)rn at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)rn at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)rn at java.lang.Thread.run(Unknown Source)rnrn我用的是spring+hibernate+webwork,在application.xml文件中已经配置了User.hbm.xml,查询语句也没写错,写的是类User不是表名。可是还是在报错,麻烦帮忙看看,谢谢了。

User is not mapped [from User]

08-23

错误语句: List users = this.hibernateTemplate.find("from User");rn相关报错:rnStruts has detected an unhandled exception:rnrnMessages: rnUser is not mapped [from User]rnUser is not mapped [from User]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: User is not mapped [from User]rnFile: org/hibernate/hql/ast/util/SessionFactoryHelper.javarnLine number: 181rnStacktracesrnrnorg.springframework.orm.hibernate3.HibernateQueryException: User is not mapped [from User]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: User is not mapped [from User]rn org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:660)rn org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)rn org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)rn org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)rn org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:912)rn org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:904)rnrnUser类:rn@Entity(name="user")rnpublic class User rn private int id;rn private String username;rn private String password;rn private String phone;rn private String addr;rnrn //get 和 set 省略rnrnrnhibernate自动的表rnrnApplicationContext.xmlrnrnrn rn rnrnrnrn rn rn rn rn rn rnrn rn rnrn rn rn com.shopping.model.Userrn rn rnrn rn rn hibernate.dialect = org.hibernate.dialect.MySQLDialectrn hibernate.show_sql = true rn hibernate.hbm2ddl.auto = creatern rn rnrn rnrn rn rn rnrnrn求解答

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试