确定hibernate中查询列的数据类型的问题 [英] problem with determining data type of query column in hibernate
问题描述
我在hibernate中写下了下面的查询,它给了我NullPointerException
SELECT new com.reddy.ReddyDTO(col1, IF(STRCMP(COL8,'zero')= 0,'string',col2))
FROM entity
然而,它抛出了NPE,因为它无法确定第二列的数据类型为字符串类型。
我可以看到它无法确定通过在 org.hibernate.hql.ast.tree.ConstructorNode.resolveConstructor(String path)
方法中查看下面位置
的类型,字段 constructorArgumentTypes 具有值
(org.hibernate.type.StringType,null)
<如何让hibernate理解这个字段是String数据类型?
(我已经试图把string放在双引号中,但是这给了我错误)
嗯...这是什么 IF
表达式?我在参考文档中找不到它。据我所知,最接近的是一个 case
表达式:
简单案件,案件......当......然后......其他......结束,搜查案件,案件......然后......其他......结束
blockquote>
但是我怀疑你可以在构造函数表达式中使用它。
$ b 更新:看起来Hibernate比我想的要好,你可以在构造函数表达式中使用一个case:SELECT new com.reddy.ReddyDTO(col1,case col8 when'zero'then'string'else col2 end)FROM entity
参考
- Hibernate核心参考指南
I have written below query in hibernate and it is giving me NullPointerException
SELECT new com.reddy.ReddyDTO( col1, IF(STRCMP(COL8, 'zero')=0, 'string', col2) ) FROM entity
However it is throwing me NPE as it was unable to determine data type for second column as String type.
I can see that it was unable to determine a type by looking at below location in
org.hibernate.hql.ast.tree.ConstructorNode.resolveConstructor(String path)
method, the fieldconstructorArgumentTypes
is having values(org.hibernate.type.StringType, null)
How to make hibernate understand this column is of String data type? (I have already trying to put "string" in double-quotes, but that is giving me error)
解决方案Hmm... What is this
IF
expression? I can't find it in the reference documentation. To my knowledge, the closest would be acase
expression:"Simple" case, case ... when ... then ... else ... end, and "searched" case, case when ... then ... else ... end
But I doubt you can use this in a constructor expression.Update: It appears Hibernate is better than I thought, you can use a case in a constructor expression:
SELECT new com.reddy.ReddyDTO(col1, case col8 when 'zero' then 'string' else col2 end) FROM entity
Reference
- Hibernate Core Reference Guide
这篇关于确定hibernate中查询列的数据类型的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!