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 (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 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(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

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 ParameterTranslationsImpl.java 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?

No comments :

Post a Comment