These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How to add capability of return null values from query...
PostPosted: Tue Mar 24, 2009 2:10 pm 
Newbie

Joined: Tue Mar 24, 2009 12:00 pm
Posts: 2
Hi guys!

I have entity Person with id, firstName, lastName, phone and description fields.
I need to get some information (for example, firstName and description or just firstName) about a person. But I can't get whole entity from datebase because I now only name of columns in the datebase.
To do this I need to create a HQL query like this
SELECT firstName, lastName, description FROM Person WHERE id = :personId

And it is quite easy and as a result I get an array of objects where
object[0] = "Andrew"
object[1] = "Ivanov"
object[2] = "hibernate in action"

But: if person does not have lastName (field have null value), I get this:
object[0] = "Andrew"
object[1] = "hibernate in action"

But I want to get following:
object[0] = "Andrew"
object[1] = null
object[2] = "hibernate in action"

How can I say hibernate to return null values? I could not find an answer to my question on the forum. I found Example and excludeNone(), but I was not able to configure them so that they meet my requirements (and I am not sure that they can be configured so).

Can you help me with my problem?

I would be grateful for you help,
Andrew.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 25, 2009 4:12 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
This seems really strange. If it really behaves like that I would conisder it a bug. The entire feature of doing scalar queries would more or less be useless.

But... I made a test of my own and I could not reproduce this. All Object-arrays are of the same size and null values appear at the expected places.

I am using Hibernate 3.3.0.sp1

Here is my test code:
Code:
org.hibernate.Query q = session.createQuery(
   "select name, description, startDate from NewsData where id=43711");
List<Object[]> l = q.list();
for (Object[] row : l)
{
   for (int i = 0; i < row.length; ++i)
   {
      System.out.println(i+"=" + row[i]);
   }
}


And the output:
Code:
0=New news
1=null
2=2009-03-25


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 25, 2009 4:20 pm 
Newbie

Joined: Tue Mar 24, 2009 12:00 pm
Posts: 2
nordborg wrote:
This seems really strange. If it really behaves like that I would conisder it a bug. The entire feature of doing scalar queries would more or less be useless.

But... I made a test of my own and I could not reproduce this. All Object-arrays are of the same size and null values appear at the expected places.

I am using Hibernate 3.3.0.sp1

Here is my test code:
Code:
org.hibernate.Query q = session.createQuery(
   "select name, description, startDate from NewsData where id=43711");
List<Object[]> l = q.list();
for (Object[] row : l)
{
   for (int i = 0; i < row.length; ++i)
   {
      System.out.println(i+"=" + row[i]);
   }
}


And the output:
Code:
0=New news
1=null
2=2009-03-25

nordborg, thanks for you post. You result is very interesting... Unfortunately, I can not tell now which version of hibernate I use at work but I reproduced similar behavior on my laptop with hibernate 3.2.6 ga.
But I found a solution. It's quite simple:
SELECT coalesce(lastName, ''), coalesce(lastName, '') FROM Person WHERE id = 1;

Perhaps this will be useful to somebody.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.