Thursday, May 18, 2023

How to Fix NullPointerException due to Space in HQL named queries in Hibernate? Example

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
                       .getNamedParameterExpectedType(ParameterTranslationsImpl.java:63)
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


It 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 the source code of getNamedParameterInfo() method, instead of focusing on the HQL query

to Fix NullPointerException due to Space in HQL named queries in Hibernate? Example


And, if you are serious about improving your Hibernate and JPA skills then I also recommend you check out these best Hibernate and JPA courses on Udemy. It's a great resource to learn Hibernate and JPA with Spring Boot in a hands-on and practical manner. 



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 to 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 errors instead of just throwing blank NullPointerException, which instead of giving some clue can very well mislead you in different directions.

Hibernate NullPointerException due to Space in HQL named queriesThis 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 gets validated at compile time instead of runtime?


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 for Beginners (books)
  • 5 Books to Learn Spring Framework for Beginners (books)
  • Why Hibernate Entity class should not be final in Java? (answer)
  • 10 Hibernate Questions from Java Interviews (list)
  • My favorite Hibernate and JPA Courses for Beginners (courses)
Thanks for reading this article, if you like this article and the interview question then please share it with your friends and colleagues. If you have any questions or feedback then please drop a comment.

No comments :

Post a Comment