如何选择条件等于null。

时间:2012-04-24 08:05:55

标签: java sql spring hibernate select

SQL语句:

select * FROM Message
 WHERE senderId = 126 and receiverId = 125
   and dateReceived is null

HQL声明:

 FROM Message
WHERE senderId = :senderId and receiverId = :receiverId
 and dateReceived is null

我收到了错误。那么如何编写HQL以获得与上述sql语句相同的结果?

3 个答案:

答案 0 :(得分:3)

尝试使用IS EMPTY应该有效。

答案 1 :(得分:2)

senderId和receiverId是外键吗? 如果是,那么假设senderId是Sender表的列,receiverId是Receiver表的列,试试这个:

select msg FROM Message msg
 WHERE msg.Sender.senderId = 126 and msg.Receiver.receiverId = 125
   and msg.dateReceived is null

OR

Select msg FROM Message msg
WHERE msg.senderId = :senderId and msg.receiverId = :receiverId
 and msg.dateReceived is null

答案 2 :(得分:0)

我已经通过

解决了这个问题

HQL查询: 来自消息 WHERE senderId =:senderId和receiverId =:receiverId 和dateReceived = null