NullpointerException with hql query in Hibernate NameGenerator
Asked Answered
F

2

6

i have an issue with hql queries which contain a null in the select, for example: "select firstname, lastname, null from Employer"

The Nullpointer comes from:

Caused by: java.lang.NullPointerException
at org.hibernate.hql.internal.NameGenerator.generateColumnNames(NameGenerator.java:27)

So the code in NameGenerator at that line:

public static String[][] generateColumnNames(Type[] types, SessionFactoryImplementor f) throws MappingException {
    String[][] columnNames = new String[types.length][];
    for ( int i = 0; i < types.length; i++ ) {
        int span = types[i].getColumnSpan( f );  // <-- line 27
        columnNames[i] = new String[span];
        for ( int j = 0; j < span; j++ ) {
            columnNames[i][j] = NameGenerator.scalarName( i, j );
        }
    }
    return columnNames;
}

I narrowed it down to the new Class NullNode (added since Hibernate 5), which simply returns null for getType():

public class NullNode extends AbstractSelectExpression {

   public Type getDataType() {
       return null;
   } 
 ....
}

So my question is.. is this a bug in hibernate or am i missusing hibernate at that place?

Flurry answered 3/3, 2020 at 13:54 Comment(4)
Take a debugger and look whih value is null types[i] or fHibernicism
types[i] is null, because getDataType() from NullNode is returning always nullFlurry
Did you ever solve this?Hardaway
Sadly, no. I ended up removing null from the select statement and dealing differently with the result.Flurry
H
0

Both the hql and jpql BNF specifications do not allow a NULL value directly for a column in the select clause (only in the update statement BNF I could find NULL as an allowed value).

However, hibernate seems to support the null column if you specify the intended datatype, I successfully could use:

select cast(null as string) from Employer

In my case hibernate 5.4.32 was also pointing into this direction. I did not receive a NPE, rather then the following error:

Caused by: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.NullNode 
 \-[NULL] NullNode: 'null'
 [select null from com.example.demo.Employer]
Hebraism answered 17/8, 2021 at 13:35 Comment(0)
C
2

In my case this was happening after I added a join to another entity to the query and didn't prefix all the field names in the select clause with an entity alias

Cutlip answered 10/7, 2023 at 20:9 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Transcribe
H
0

Both the hql and jpql BNF specifications do not allow a NULL value directly for a column in the select clause (only in the update statement BNF I could find NULL as an allowed value).

However, hibernate seems to support the null column if you specify the intended datatype, I successfully could use:

select cast(null as string) from Employer

In my case hibernate 5.4.32 was also pointing into this direction. I did not receive a NPE, rather then the following error:

Caused by: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.NullNode 
 \-[NULL] NullNode: 'null'
 [select null from com.example.demo.Employer]
Hebraism answered 17/8, 2021 at 13:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.