org.hibernate.hql.ast.QuerySyntaxException: tablename is not mapped ......

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

org.hibernate.hql.ast.QuerySyntaxException: tablename is not mapped ...异常总结

最近在学习三大框架,一次用hibernate框架时,遇到了这个问题,检查了N遍代码,一直没有找出原因,百思不得其解,在经过很长时间的排错后发现,遇到这个问题的原因有如下几点:

1,hibernate配置文件中没有添加实体类相应的映射文件,比如<mapping resource="....."/>,这个错误一般情况下不会犯,即使没写很快也能找出错误来的
2,第二个是非常容易出错的,在写hql 语句的时候,比如  hql = " from user ";  这个 from 后面接的是你实体类的类名,而不是数据库中的表名,这个要注意了!!!而且必须要区分大小写!!!本人因为就是因为这个问题困惑了很久......
3,最后一个注意hql语句一定要写对哦!

(Annotation)org.hibernate.hql.ast.QuerySyntaxException: XX is not mapped ! 请高手解决

02-20

Spring配置文件:rnrn rn com.modelrn rnrn....rnrnUser类:rn@Entity(name="sys_user")rnpublic class User rn private int id;rn private String name;rn private String password;rn private Role role;rn private int flag;rn....rnrnHQL语句:rn"from User"rnrnrn错误:rnorg.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 at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:657)rn at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)rn at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)rn at org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:343)rn at com.util.PageDaoImpl.getListForPage(PageDaoImpl.java:41)rn at com.service.impl.UserServiceImplTest.testListForPage(UserServiceImplTest.java:80)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)rn at java.lang.reflect.Method.invoke(Method.java:597)rn at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)rn at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)rn at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)rn at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)rn at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)rn at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)rn at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)rn at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)rn at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)rn at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)rn at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)rn at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)rn at org.junit.runners.ParentRunner.run(ParentRunner.java:236)rn at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)rn at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)rn at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)rn at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)rn at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)rn at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)rnCaused by: org.hibernate.hql.ast.QuerySyntaxException: User is not mapped [from User]rn at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181)rn at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)rn at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:94)rn at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:316)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3228)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3112)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:571)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:288)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:231)rn at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)rn at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)rn at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)rn at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:101)rn at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:80)rn at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)rn at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)rn at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)rn at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1651)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)rn at java.lang.reflect.Method.invoke(Method.java:597)rn at org.springframework.orm.hibernate3.HibernateTemplate$CloseSuppressingInvocationHandler.invoke(HibernateTemplate.java:1293)rn at $Proxy18.createQuery(Unknown Source)rn at com.util.PageDaoImpl$1.doInHibernate(PageDaoImpl.java:44)rn at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)rn ... 26 morernrn谁知道请回一下, 我在线等。我猜是Annotation的问题

org.hibernate.hql.ast.QuerySyntaxException

10-29

="1.0" encoding="UTF-8"?>rnrnrn rn org.hibernate.ejb.HibernatePersistencern rn rn rn rn rn rn rn rn rn rn rn rnrnrn=============================================================================================rnrnpackage com.fengmanfei.student.entity;rnrnimport java.io.Serializable;rnimport java.util.HashSet;rnimport java.util.Set;rnrnimport javax.persistence.Column;rnimport javax.persistence.Entity;rnimport javax.persistence.FetchType;rnimport javax.persistence.GeneratedValue;rnimport javax.persistence.GenerationType;rnimport javax.persistence.Id;rnimport javax.persistence.OneToMany;rnimport javax.persistence.Table;rnrn@Entity(name = "tb_class")rn//@Table(name = "tb_class")rnpublic class ClassEO implements Serializable rnrn private static final long serialVersionUID = 4231121505530870021L;rnrn @Idrn @GeneratedValue(strategy = GenerationType.AUTO)rn private Integer id;rnrn @Columnrn private String name;rnrn @OneToMany(fetch = FetchType.LAZY, mappedBy = "classEO")rn private Set students = new HashSet();rnrn public Integer getId() rn return id;rn rnrn public void setId(Integer id) rn this.id = id;rn rnrn public String getName() rn return name;rn rnrn public void setName(String name) rn this.name = name;rn rnrn public Set getStudents() rn return students;rn rnrn public void setStudents(Set students) rn this.students = students;rn rnrnrnrn=============================================================================rnpackage com.fengmanfei.student.servlet;rnrnimport java.io.IOException;rnimport java.util.List;rnrnimport javax.persistence.EntityManager;rnimport javax.persistence.EntityManagerFactory;rnimport javax.persistence.EntityTransaction;rnimport javax.persistence.Persistence;rnimport javax.persistence.Query;rnimport javax.servlet.ServletException;rnimport javax.servlet.http.HttpServlet;rnimport javax.servlet.http.HttpServletRequest;rnimport javax.servlet.http.HttpServletResponse;rnrnimport com.fengmanfei.student.entity.ClassEO;rnimport com.fengmanfei.student.exception.StudentException;rnrnpublic class ClassServlet extends HttpServlet rnrn private static final long serialVersionUID = -8205614348005279786L;rnrn // @PersistenceUnit(unitName="student")rn private EntityManagerFactory emf;rnrn @Overridern public void init() throws ServletException rn if (emf == null) rn emf = Persistence.createEntityManagerFactory("student");rn rn rnrn @SuppressWarnings("unchecked")rn public void doGet(HttpServletRequest request, HttpServletResponse response)rn throws ServletException, IOException rnrn String action = request.getParameter("action");rnrn if ("list".equals(action)) rnrn EntityManager em = emf.createEntityManager();rnrn try rn // 读取数据库时不需要事务,直接查询即可ClassEOrn Query query = em.createQuery(" from ClassEO order by id asc ");rnrn List classList = query.getResultList();rnrn request.setAttribute("classList", classList);rn request.getRequestDispatcher("/listClass.jsp").forward(request,rn response);rn finally rn em.close();rn rn else if ("add".equals(action)) rnrn String name = request.getParameter("name");rnrn // 如果没有输入名称,则抛出StudentException异常。rn if (name == null || name.trim().length() == 0) rn throw new StudentException("请填写班级名称");rn rnrn // 获取EntityManager对象rn EntityManager em = emf.createEntityManager();rnrn // JPA写数据库必须开启事务。rn EntityTransaction trans = em.getTransaction();rnrn try rn // 事务开始rn trans.begin();rnrn // 查找同名的ClassEOrn List classList = em.createQuery(rn " from ClassEO where name = :name ").setParameter(rn "name", name).getResultList();rnrn // 如果已经存在,则报错rn if (classList.size() > 0) rn throw new StudentException("班级已经存在:" + name);rn rnrn // new一个新的Entity Objectrn ClassEO classEO = new ClassEO();rnrn // 设置名称。ID有数据库自动生成。rn classEO.setName(name);rnrn // persistrn em.persist(classEO);rnrn // 事务提交。如果前面或者这时有异常抛出,会自动回滚(Rollback)。rn trans.commit();rnrn // 成功信息rn request.setAttribute("msg", "班级 " + classEO.getName()rn + " 添加成功。");rnrn // 显示成功页面rn request.setAttribute("classEO", classEO);rn request.getRequestDispatcher("/addClassSuccess.jsp").forward(rn request, response);rnrn finally rn // EntityManager 对象关闭rn em.close();rn rnrn else if ("edit".equals(action)) rnrn EntityManager em = emf.createEntityManager();rnrn try rn // 要修改的班级IDrn int classId = Integer.parseInt(request.getParameter("classId"));rnrn // 查找班级实体rn ClassEO classEO = em.find(ClassEO.class, classId);rnrn request.setAttribute("classEO", classEO);rnrn request.getRequestDispatcher("/addClass.jsp").forward(request,rn response);rnrn finally rn em.close();rn rn else if ("save".equals(action)) rnrn EntityManager em = emf.createEntityManager();rnrn // 写数据库,需要事务rn EntityTransaction trans = em.getTransaction();rnrn try rn // 事务开始rn trans.begin();rnrn // 修改的是哪个班级rn int classId = Integer.parseInt(request.getParameter("classId"));rnrn String name = request.getParameter("name");rnrn if (name == null || name.trim().length() == 0) rn throw new StudentException("请填写班级名称");rn rnrn List list = em.createQuery(rn " from ClassEO where name = :name and id <> :id ")rn .setParameter("name", name).setParameter("id", classId)rn .getResultList();rnrn if (list.size() > 0) rn throw new StudentException("修改后的班级名称与其他班级冲突,修改失败");rn rnrn // 查找班级实体rn ClassEO classEO = em.find(ClassEO.class, classId);rnrn // 修改属性rn classEO.setName(name);rnrn em.merge(classEO);rnrn trans.commit();rnrn request.setAttribute("msg", "班级 " + classEO.getName()rn + " 修改成功。");rnrn request.setAttribute("classEO", classEO);rn request.getRequestDispatcher("/addClassSuccess.jsp").forward(rn request, response);rn finally rn em.close();rn rnrn else if ("delete".equals(action)) rnrn EntityManager em = emf.createEntityManager();rnrn EntityTransaction trans = em.getTransaction();rnrn try rnrn trans.begin();rnrn int classId = Integer.parseInt(request.getParameter("classId"));rnrn ClassEO classEO = em.find(ClassEO.class, classId);rnrn if (classEO.getStudents().size() > 0) rn throw new StudentException("班级 " + classEO.getName()rn + " 还有 " + classEO.getStudents().size()rn + " 个学生,不能删除。");rn rnrn em.remove(classEO);rnrn trans.commit();rnrn request.setAttribute("msg", "班级 " + classEO.getName()rn + " 删除成功。");rnrn request.setAttribute("classEO", classEO);rn request.getRequestDispatcher("/addClassSuccess.jsp").forward(rn request, response);rnrn finally rn em.close();rn rn rnrn rnrn public void doPost(HttpServletRequest request, HttpServletResponse response)rn throws ServletException, IOException rnrn this.doGet(request, response);rn rnrn @Overridern public void destroy() rn // 销毁 EntityManagerFactoryrn emf.close();rn rnrnrn========================================================================rnrnrnJPA详解的例子,部署到jboss中后,显示:java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: ClassEO is not mapped [ from ClassEO order by id asc ] rnrn用的是myeclipse8.5、jboss5.0、mysql5.1,网上查找到的:都说jpa操作的是实体类,不是表,这显然是对的。请高手指教。

org.hibernate.hql.ast.QuerySyntaxException:

04-19

我在测试使用hibernate过程中,执行hql语句,后台报了这个错,给位大侠帮我分析分析~小弟感激不尽rn后台错误code:rn2012-4-19 16:39:15 org.apache.catalina.core.StandardWrapperValve invokern严重: Servlet.service() for servlet jsp threw exceptionrnorg.hibernate.hql.ast.QuerySyntaxException: User is not mapped [select count(*) from User u where u.username = :username]rn at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181)rn at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)rn at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)rn at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:277)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)rn at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:251)rn at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)rn at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)rn at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:101)rn at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:80)rn at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)rn at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)rn at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)rn at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)rn at java.lang.reflect.Method.invoke(Method.java:597)rn at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)rn at $Proxy0.createQuery(Unknown Source)rn at com.bjsxt.register.mager.UserManager.exists(UserManager.java:29)rn at org.apache.jsp.RegisterDeal_jsp._jspService(RegisterDeal_jsp.java:73)rn at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)rn at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)rn at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)rn at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)rn at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)rn at java.lang.Thread.run(Thread.java:619)rn

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

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

关闭