Reputation: 21329
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 ?
Upvotes: 0
Views: 105
Reputation: 35597
Query you are going to use is not a HQL
query
From librarian
will work
Upvotes: 0
Reputation: 17622
use librarian
is not a valid query and its not returning any result.
Try some valid query as 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.
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.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
Upvotes: 1
Reputation: 34424
It should be
String hqlSelectDB = "from librarian";// where librarian is data object mapped with DB table
sess.createQuery(hqlSelectDB).list();
Upvotes: 0