Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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

Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ";
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(
at org.hibernate.hql.ast.QuerySyntaxException.convert(
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(
at org.hibernate.engine.query.HQLQueryPlan.<init>(
at org.hibernate.engine.query.HQLQueryPlan.<init>(
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(
at org.hibernate.impl.AbstractSessionImpl.createQuery(
at org.hibernate.impl.SessionImpl.createQuery(

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

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:
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!