tags:

views:

67

answers:

1

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 field constructorArgumentTypes 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)

+1  A: 

Hmm... What is this IF expression? I can't find it in the reference documentation. To my knowledge, the closest would be a case 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

Pascal Thivent
hmm... I am using MySQL and, first part in the IF is condition, second value is if the condition is true, and other one is if condition is false. Will check with case also, if it allows me to compare string values.
Reddy
@Reddy You're using HQL here - not native SQL - and HQL doesn't have an IF expression.
Pascal Thivent
thanks a lot Pascal, it worked with case statement.
Reddy
@Reddy You're welcome. Glad it's solved.
Pascal Thivent