Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I select null as a column value in HQL query?

I need a list with three columns. column 1st and 3rd having values while 2nd as null. Can I do it through HQL query?

I need something like this:

select id, null, name from MyClass

Where MyClass as well as underlying table has only two properties/columns ie, "id" and "name"

like image 814
WSK Avatar asked Jul 30 '10 22:07

WSK


2 Answers

You can use nullif() function to generate null.

It accepts two arguments. If arguments are equal, it'll return null value.

like image 61
sohrab yousefi Avatar answered Sep 30 '22 02:09

sohrab yousefi


Another option that seems to work (tested on DB2, MySQL, Oracle, and SQL Server):

select id, cast(null as char), name from ...

You could subclass the Hibernate dialect and abstract it with a custom function:

registerFunction("always_null", 
  new SQLFunctionTemplate(Hibernate.STRING, "cast(null as char)"));

and then use this in your HQL:

select id, always_null(), name from ...
like image 30
Brian Deterling Avatar answered Sep 30 '22 03:09

Brian Deterling