Friday, March 28, 2014

Hibernate NullPointerException due to Space in HQL named queries

If you are using Hibernate for implementing persistence layer in Java and JEE application from couple of years then you would have seen this notorious NullPointerException while executing HQL named queries, Exception in thread “main” java.lang.NullPointerException at org.hibernate.hql.ast.ParameterTranslationsImpl .getNamedParameterExpectedType (  Hibernate has some poor logging in case of Exception, which has caused me hours to debug a simple problem. By looking at NullPointerException below (look full stacktrace below), I had no clue that it's coming because of a missing space on HQL (Hibernate Query language) query. You can also take a look if you can figure this out :

Exception in thread main java.lang.NullPointerException
at org.hibernate.hql.ast.ParameterTranslationsImpl.getNamedParameterExpectedType(
at org.hibernate.engine.query.HQLQueryPlan.buildParameterMetadata(
at org.hibernate.engine.query.HQLQueryPlan.(
at org.hibernate.engine.query.HQLQueryPlan.(
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(

Hibernate NullPointerException due to Space in HQL named queriesIt would have been lot better, if they have printed name of variable which is null or anything which points to right direction, here you only have a clue that something happened at at line 63. Now by looking at code, you do get impression that getNamedParameterInfo(name) is returning null and then you go to check source code of getNamedParameterInfo() method, instead of focusing on HQL query

public Type getNamedParameterExpectedType(String name) {
    return getNamedParameterInfo( name ).getExpectedType();

After spending some time I found that it was happening due to an extra space given after the colon in the hibernate query (HQL). For example we had this as query String

select EMP_ADDRESS from Employee where EMP_ID = :  ID

Do you notice the extra space? Well it's not that easy to spot this kind of error. Now all you need to do to remove this NullPointerException is that to remove that extra white space from HQL query String i.e.

select EMP_ADDRESS from Employee where EMP_ID = : ID

So always put attention what you put between colon and parameter in named SQL queries. Of course Hibernate can do better job to provide more information while reporting error instead of just throwing blank NullPointerException, which instead of giving some clue can very well mislead you into different direction. This is the problem with any language, they can not validate anything which is inside String, wouldn't it be better if your HQL are also get validated at compile time instead of runtime?

Further Learning
Introduction To Hibernate
Spring with JPA and Hibernate
Hibernate Performance tuning

Other Hibernate Articles and Interview Questions you may like
  • Difference between First and Second level cache in Hibernate? (answer)
  • Difference between get() and load() method in Hibernate? (answer)
  • 5 Spring and Hibernate Training Courses for Java developers (list)
  • 2 Books to Learn Hibernate in 2017 (books)
  • 5 Books to Learn Spring Framework in 2017 (books)
  • Why Hibernate Entity class should not be final in Java? (answer)
  • 10 Hibernate Questions from Java Interviews (list)

Thanks for reading this article, if you like this article and interview question then please share with your friends and colleagues. If you have any question or feedback then please drop a comment.

No comments :

Post a Comment