简体   繁体   English

我尝试运行查询时遇到异常。 这是为什么?

[英]Getting an exception as I try to run a query. Why is that?

The following snippet throws an exception : 以下代码段引发异常:

String hqlSelectDB = "use librarian";
sess.createQuery(hqlSelectDB).list();

Exception 例外

java.lang.IllegalArgumentException: node to traverse cannot be null!
org.hibernate.hql.ast.util.NodeTraverser.traverseDepthFirst(NodeTraverser.java:31)
org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:254)
org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157)
org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
servlets.InsertPerson.doPost(InsertPerson.java:41)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

Why am I getting this exception ? 为什么会出现此异常?

use librarian is not a valid query and its not returning any result. use librarian不是有效的查询,它不返回任何结果。

Try some valid query as from librarian.SomeTable t 尝试from librarian.SomeTable t一些有效的查询

If you have a look at Hibernate Configuration , you need to specify which DB you want to use, during configuration phase itself. 如果您查看Hibernate Configuration ,则需要在配置阶段本身期间指定要使用的数据库。

This is the example of hibernate configuration property file, which will be used by hibernate to make a SessionFactory (A kind of Connection pool), which will give the DB Connection. 这是hibernate配置属性文件的示例,hibernate将使用它来创建一个SessionFactory (一种连接池),它将提供数据库连接。

hibernate.connection.driver_class = org.postgresql.Driver
hibernate.connection.url = jdbc:postgresql://localhost/mydatabase --> This is where you specify your DB name
hibernate.connection.username = myuser
hibernate.connection.password = secret
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

It should be 它应该是

String hqlSelectDB = "from librarian";// where librarian is data object mapped with DB table
sess.createQuery(hqlSelectDB).list();

Query you are going to use is not a HQL query 您要使用的查询不是HQL查询

From librarian

will work 将工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在尝试解析.docx文件时出现异常? - Why am I getting an exception as I try to parse .docx file? 为什么在尝试读取.DOCX文件时出现异常? - Why am I getting exception as I try to read .DOCX file? Postgres异常:查询未返回任何结果。 - Postgres Exception : No results were returned by the query. 尝试删除“活动”上的操作栏时,为什么会出现此异常? - Why am I getting this Exception when I try to remove Action Bar on my Activity? 在尝试使用WHERE子句时获取异常 - Getting exception as I try use the WHERE clause 当我尝试运行此程序时,为什么会有(java.lang.ArrayIndexOutOfBoundsException:5)异常? - why there is an (java.lang.ArrayIndexOutOfBoundsException: 5) exception when I try to run this program? 我收到一个错误:“异常 FileNotFoundException 永远不会在相应的 try 语句的主体中抛出”并且无法弄清楚原因 - I'm getting an error: “exception FileNotFoundException is never thrown in body of corresponding try statement” and cannot figure out why 尝试在本地主机上运行Web应用程序时,为什么会出现java.lang.UnsupportedClassVersionError? - Why am I getting java.lang.UnsupportedClassVersionError as I try to run the web application on localhost? 当我尝试在 Java 中运行我的套接字程序时,为什么会出现错误? - Why am I getting an error when I try to run my socket program in Java? 尝试运行JFrame时为什么会出现NullPointerException? - Why am I getting a NullPointerException when I try to run my JFrame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM