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?
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]