org.hibernate.hql.internal.ast.QuerySyntaxException: userinfo is not mapped [from userinfo u where u

    xiaoxiao2021-03-25  5

    今天在进行一对多查询,然后打印结果集的时候出了bug:

    Userinfo u = new Userinfo(); String hql="from userinfo u where u.phone=:phone"; Query query=session.createQuery(hql).setParameter("phone", tphone); List<Userinfo> list =query.list(); System.out.println(list);

    org.hibernate.hql.internal.ast.QuerySyntaxException:userinfo is not mapped [from userinfo u where u.phone= :phone]

          atorg.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)

          atorg.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)

          atorg.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)

          atorg.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:324)

          atorg.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3270)

          atorg.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3159)

          atorg.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706)

          atorg.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562)

          atorg.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)

          atorg.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)

          atorg.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)

          atorg.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)

          atorg.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)

          atorg.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)

          atorg.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)

          atorg.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)

          atorg.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:219)

          atorg.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:197)

          atorg.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1736)

          atTest.doNewAccount.fun4(doNewAccount.java:29)

          atsun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

          atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

          atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

          atjava.lang.reflect.Method.invoke(Method.java:606)

          atorg.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)

          atorg.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

          atorg.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)

          atorg.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

          atorg.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)

          atorg.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)

          atorg.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)

          atorg.junit.runners.ParentRunner$3.run(ParentRunner.java:238)

          atorg.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)

          atorg.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)

          atorg.junit.runners.ParentRunner.access$000(ParentRunner.java:53)

          atorg.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)

          atorg.junit.runners.ParentRunner.run(ParentRunner.java:309)

          atorg.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)

          atorg.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

          atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)

          atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)

          atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)

          atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

    然后网上查了一下:

    【http://blog.csdn.net/edward_qing_lee/article/details/7753030】

    看了一下这个,觉得是不是我的表名写错了?

    虽然数据库不分大小写,但是类名是要分的啊!!

    果然,当我把类名改成首字母大写之后,就不报错了,哎!!!!!面向对象编程啊!!!

    忘记数据库吧!!!!!!

    最后可用的代码是:

    Userinfo u = new Userinfo(); String hql="select count(*) from Userinfo u where u.phone= :phone"; Query query=session.createQuery(hql).setParameter("phone", tphone); List<Userinfo> list =query.list(); System.out.println(list);

    然后继续报错:

    使用query.uniqueResult()的时候总是说它不能强制转换,那我怎么判断啊!

    哎!又跪在equals方法,我真的是醉了。

    public class isExist { private boolean b=false; String tphone="1110"; @Test public void isPhoneExist(){ Configuration conf = new Configuration().configure(); SessionFactory sf = conf.buildSessionFactory(); Session session = sf.openSession(); Transaction ts = session.beginTransaction(); Userinfo u = new Userinfo(); String hql="select count(*) from Userinfo u where u.phone= :phone"; Query query=session.createQuery(hql).setParameter("phone", tphone); Object ui=query.uniqueResult(); if(ui.toString().equals("0")){System.out.println("nono");} System.out.println(ui.toString()); }

    走到了这里,我以为没啥问题了,能打印了。

    但是结果不对,结果只有一个。 我百思不得其解。

    回去问了女神,女神说过程没问题,让我看一下list的size。 我输出了一下,果然是1.

    最后我实在查不到错了,我觉得过程没问题。我把前面的代码都注销了,就跑这两句,一下就出 红色的感叹号了,

    哎呀妈呀!!!!query和query2!!!!!!

    命名不规范真的吃好大亏!!

    转载请注明原文地址: https://ju.6miu.com/read-200101.html

    最新回复(0)