-->
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.  [ 2 posts ] 
Author Message
 Post subject: NULL elements in collection retrieved from DB
PostPosted: Mon Sep 07, 2009 1:13 pm 
Newbie

Joined: Mon Sep 07, 2009 12:55 pm
Posts: 2
Hi

I have found an issue that I am struggling to find a way around.

We have a simple class that encapsulates two Double fields. These are themselves stored in a collection (a List) to be stored in the database.

What I have found is that if the List contains a single element in which both double fields are 'null', when I then query for the data back from the database I get a List with a single 'null' element, rather than an element of my type with the two null doubles inside. Just to be clear, here is the sequence of events:

>>>>> Lets assume my class with the two doubles is Bob:
Quote:
class Bob {
Double a = null;
Double b = null;


public Bob(Double a, Double b) {
this.a = a;
this.b = b;
}

//getters and setters ommited
}

>>>>> I then have a list as this:
Quote:
Bob bob = new Bob(null,null);
List<Bob> myList = new ArrayList<Bob>();
myList.add(bob);

>>>>> I then proceed to store myList. I can see in the database that Bob is indeed stored, it has an ID as expected, and all is good, a and b are both null.
When I subsequently retrieve List<Bob> from the database, I end up with a list with a single element inside it, but this element is null, whereas I was expecting a Bob with null in its two Double values.

I have loaded the hibernate source code to try and figure out what is going on here. Here is an extract of the ComponentType class, line 570-596 (version 3.3.2.GA):
Quote:
public Object hydrate(
final ResultSet rs,
final String[] names,
final SessionImplementor session,
final Object owner)
throws HibernateException, SQLException {

int begin = 0;
boolean notNull = false;
Object[] values = new Object[propertySpan];
for ( int i = 0; i < propertySpan; i++ ) {
int length = propertyTypes[i].getColumnSpan( session.getFactory() );
String[] range = ArrayHelper.slice( names, begin, length ); //cache this
Object val = propertyTypes[i].hydrate( rs, range, session, owner );
if ( val == null ) {
if ( isKey ) {
return null; //different nullability rules for pk/fk
}
}
else {
notNull = true;
}
values[i] = val;
begin += length;
}

return notNull ? values : null;
}


This 'hydrate' method is being invoked to process the resultset from the table that contains my Bob-s. But it appears that unless (val == null) is false at least once, notNull will always be 'false', which means that the method will return 'null' as opposed to an Object array with two 'null' elements in it.

I have proved this by adding a third property in Bob, say 'c', and making sure this property is never 'null' by initializing it in to a value (not null). In that case the object is created as expected and populated when retrieved from the database, with 'a' and 'b' being set to null, and 'c' to its expected value.

Please forgive me if I am making a glaring mistake in my approach, but I am now stuck with this one.

a) Is this behaviour expected or is this a bug?
b) what is the correct way to get around this issue?

Many thanks in advance for your help, and please let me know if you need any other info


Top
 Profile  
 
 Post subject: Re: NULL elements in collection retrieved from DB
PostPosted: Tue Sep 08, 2009 5:34 pm 
Newbie

Joined: Mon Sep 07, 2009 12:55 pm
Posts: 2
Nobody?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.