开发者

Hibernate HQL Query Null Columns

I have a simple HQL SELECT query. When the columns are not null, I'm able to retrieve the records. But if one of the columns are null, I don't get that record that has a null column.

Here's a sample HQL query:

SELECT p.name as name, p.price as price
FROM  Pets p

If name or price is null, I don't get any entry for that particular record. I thought Hibernate will just return a null value for that column when it's null?

How can I provide an alternate value when the column is null?

I have开发者_运维技巧 tested this with Hibernate Console.


Two things to confirm:

  1. Your model is named Pets, right? Or is it Pet?
  2. Do you really want only those two properties? The usual way is to retrieve the full object, like from Pet.

But even that should work fine (provided that the model is named Pets). Do you get any exceptions, or you just get an empty List? What's the SQL that hibernate is generating (you can see that by setting the property hibernate.show_sql to true). If you run this SQL by hand in your database, do you get any records?


It might be a late reply. But hope it help some one else!

I think here the hibernate is giving you the List of Object array with all NULL items. If you want to see that just try to print the size of the result list and array to know it.

One more way is to modify your code like below

SELECT new Pets(p.name, p.price)
FROM  Pets p

Also add a constructor for Pets bean to set name and price.

Now you can see the Pets beans in the list with null values set for name and price. See here for more in partial initialization of the beans in HQL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜