How to Solve QuerySyntaxException in Hibernate?

Krishna Srinivasan
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.
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)
I hope this solution would solve your problem. Still you have not solved, please post your exception trace in the comments section.

Comments