5

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?

Johannes Hinkov
  • 761
  • 8
  • 9

2 Answers2

1

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

  • 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](/help/how-to-answer). – Community Jul 11 '23 at 17:41
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]
tscz
  • 113
  • 7