JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

How to Solve QuerySyntaxException in Hibernate?

September 26, 2015 by Krishna Srinivasan Leave a Comment

If you are working on hibernate, getting QuerySyntaxException is very common if there is any issue with the query which you are trying to execute in your code. This QuerySyntaxException is more generic and it would throw different types of error messages based on the issue with the query. In this post I would update my experience on getting the different type of messages and how I have solved them.

QuerySyntaxException : Table is not mapped

I have encountered this exception while using the HSQL databases for running my Spring Data application using Spring Boot. Also this error thrown at the time of using the query methods to write the query in spring data repository.

  • Error Message: org.hibernate.hql.internal.ast.QuerySyntaxException: $$table_name$$ user is not mapped [$$query_string$$]
  • Problem: This problem occurs only when the table name you have mentioned in the query is not matching with the entity.
  • Solution: The table name used in the query is not the one in the real database, it should be the name of the entity class. If you are trying to map to the database table and actual entity class name is different, it would throw this error. Also note that the query string should have the exact case (Uppercase or Lowercase) of the entity class name to work properly.

[code]
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: user is not mapped [select username from user u where u.username = ?1]
at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: user is not mapped
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:95)
[/code]

I hope this solution would solve your problem. Still you have not solved, please post your exception trace in the comments section.

Filed Under: Hibernate Tagged With: Hibernate Exceptions

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved