Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference in session.createQuery() and session.createSqlQuery()  RSS feed

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can someone please assist me to gunderstand the core difference between esession.createQuery() and session.createSqlQuery(). When I run this following code it gives error


final String queryString = " Select c.tradingName,c.contactName from Client c where c.clientId=111";
final Session session = HibernateSessionFactory.getCurrentSession();
List q = (session.createQuery(queryString)).list();
Iterator it = q.iterator();
while (it.hasNext()) {
System.out.println(" *************** while executing query "+it.next());
}
return q;


it gives error:-

Caused by: org.hibernate.hql.ast.QuerySyntaxException: expecting IDENT, found '*' near line 1, column 11 [ Select c.* from com.rbsfm.clientvaluations.objects.Client c where c.clientId=111]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:31)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:24)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:258)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)


But while I replace the session.createQuery() by session.createSqlQuery() it works fine. Why ??

thanks
RK
 
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
createQuery creates a query usign HQL, createSQLQuery creates a query using SQL. Normally you would not call createSQLQuery unless you had some functionality you absolutely could not do in HQL (or via Criteria).

Looking at that bit of code and the error you see - are you sure its that bit that is causing the error? The query you are really running looks like it is:
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!