Saturday, April 27, 2024
HomeJavaThe best way to Repair NullPointerException because of Area in HQL named...

The best way to Repair NullPointerException because of Area in HQL named queries in Hibernate? Instance


If you’re utilizing Hibernate for implementing the persistence layer in Java and JEE software from a few years then you definitely would have seen this infamous NullPointerException whereas executing HQL named queries, Exception in thread “important” java.lang.NullPointerException at org.hibernate.hql.ast.ParameterTranslationsImpl .getNamedParameterExpectedType (ParameterTranslationsImpl.java:63).  Hibernate has some poor logging in case of Exception, which has triggered me hours to debug a easy drawback. By taking a look at NullPointerException under (look full stack hint under), I had no clue that it is coming due to a lacking house on the HQL (Hibernate Question language) question. 
You can even have a look should you can determine this out :

Exception in thread important java.lang.NullPointerException

at org.hibernate.hql.ast.ParameterTranslationsImpl
                       .getNamedParameterExpectedType(ParameterTranslationsImpl.java:63)
at org.hibernate.engine.question.HQLQueryPlan.buildParameterMetadata(HQLQueryPlan.java:296)
at org.hibernate.engine.question.HQLQueryPlan.(HQLQueryPlan.java:97)
at org.hibernate.engine.question.HQLQueryPlan.(HQLQueryPlan.java:56)
at org.hibernate.engine.question.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 might have been rather a lot higher if they’ve printed the title of variables which is null or something which factors to the fitting path, right here you solely have a clue that one thing occurred at ParameterTranslationsImpl.java at line 63.

Now by taking a look at code, you do get the impression that getNamedParameterInfo(title) is returning null and then you definitely go to test the supply code of getNamedParameterInfo() methodology, as an alternative of specializing in the HQL question

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


And, if you’re critical about bettering your Hibernate and JPA abilities then I additionally suggest you take a look at these greatest Hibernate and JPA programs on Udemy. It is an important useful resource to study Hibernate and JPA with Spring Boot in a hands-on and sensible method. 

public Sort getNamedParameterExpectedType(String title) {
    return getNamedParameterInfo( title ).getExpectedType();
}

After spending a while I discovered that it was taking place because of an additional house given after the colon within the hibernate question (HQL). For instance, we had this as a Question String

choose EMP_ADDRESS from Worker the place EMP_ID = :  ID

Do you discover the additional house? Effectively, it is not that straightforward to identify this sort of error. Now all it’s essential to do to take away this NullPointerException is that to take away that additional white house from HQL question String i.e.

choose EMP_ADDRESS from Worker the place EMP_ID = : ID

So all the time put consideration to what you place between colon and parameter in named SQL queries. After all, Hibernate can do a greater job to offer extra info whereas reporting errors as an alternative of simply throwing clean NullPointerException, which as an alternative of giving some clue can very nicely mislead you in numerous instructions.

Hibernate NullPointerException due to Space in HQL named queriesThat is the issue with any language, they cannot validate something which is inside String, would not it’s higher in case your HQL can also be will get validated at compile time as an alternative of runtime?

Different Hibernate Articles and Interview Questions chances are you’ll like

  • Distinction between First and Second stage cache in Hibernate? (reply)
  • Distinction between get() and cargo() methodology in Hibernate? (reply)
  • 5 Spring and Hibernate Coaching Programs for Java builders (record)
  • 2 Books to Be taught Hibernate for Newcomers (books)
  • 5 Books to Be taught Spring Framework for Newcomers (books)
  • Why Hibernate Entity class shouldn’t be last in Java? (reply)
  • 10 Hibernate Questions from Java Interviews (record)
  • My favourite Hibernate and JPA Programs for Newcomers (programs)

Thanks for studying this text, should you like this text and the interview query then please share it with your folks and colleagues. In case you have any questions or suggestions then please drop a remark.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments