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