Hibernate NullPointerException due to Space in HQL named queries

If you are using Hibernate for implementing the persistence layer in Java and JEE application from a 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 (ParameterTranslationsImpl.java:63).  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 stack trace below), I had no clue that it's coming because of a missing space on the 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
at org.hibernate.engine.query.HQLQueryPlan.buildParameterMetadata(HQLQueryPlan.java:296)
at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:97)
at org.hibernate.engine.query.HQLQueryPlan.(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

Hibernate NullPointerException due to Space in HQL named queriesIt would have been a lot better if they have printed the name of variables which is null or anything which points to the right direction, here you only have a clue that something happened at ParameterTranslationsImpl.java at line 63.

Now by looking at code, you do get the impression that getNamedParameterInfo(name) is returning null and then you go to check source code of getNamedParameterInfo() method, instead of focusing on HQL query

And, if you are serious about improving your Hibernate and JPA skills then I also recommend you check out Master Hibernate and JPA with Spring Boot in 100 Steps course by Ranga Rao Karnam on Udemy. It's a great course to learn Hibernate and JPA with Spring Boot in a hands-on and practical manner. It's also very affordable and you can buy in just $10 on Udemy flash sales.

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 a 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 a 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 in different directions.

This is the problem with any language, they can not validate anything which is inside String, wouldn't it be better if your HQL is also get validated at compile time instead of runtime?

Further Learning
Introduction To Hibernate
Spring and Hibernate for Beginners
Hibernate Interview Questions Preparation Course

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 questions or feedback then please drop a comment.

No comments :

Post a Comment