[英]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.