Joh*_*kov 6 java orm nullpointerexception hibernate-5.x
我在选择中包含 null 的 hql 查询时遇到问题,例如:“从雇主中选择名字、姓氏、null”
空指针来自:
Caused by: java.lang.NullPointerException
at org.hibernate.hql.internal.NameGenerator.generateColumnNames(NameGenerator.java:27)
Run Code Online (Sandbox Code Playgroud)
所以 NameGenerator 中该行的代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
我将其范围缩小到新的NullNode类(从 Hibernate 5 开始添加),它只是为 getType() 返回 null:
public class NullNode extends AbstractSelectExpression {
public Type getDataType() {
return null;
}
....
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是..这是休眠中的一个错误还是我在那个地方错过了休眠?
小智 0
hql 和 jpql BNF 规范都不允许 select 子句中的列直接使用 NULL 值(只有在 update 语句 BNF 中我才能找到 NULL 作为允许值)。
但是,如果您指定预期的数据类型,hibernate 似乎支持空列,我可以成功地使用:
select cast(null as string) from Employer
Run Code Online (Sandbox Code Playgroud)
就我而言,hibernate 5.4.32 也指向这个方向。我没有收到 NPE,而是收到以下错误:
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]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3332 次 |
| 最近记录: |