The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

felipeal (1) [Avatar] Offline
#1
Hi all,

I think I found a severe bug on the examples used to explain the UserType (at pages 204-206). The problem is on the implementation of the nullSafeGet(), which calls ResultSet.wasNull() *before* calling any getter on the ResultSet.

According to the JDK API (http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultSet.html#wasNull()), the meaning of this method is:

Reports whether the last column read had a value of SQL NULL. Note that you must first call one of the getter methods on a column to try to read its value and then call the method wasNull to see if the value read was SQL NULL.


I found this issue after I implemented a custom CompositeUserType following the book's sample. It worked for a while, but then one day the call to wasNull() was returning true, which took us a while to found the problem (i.e., that that call was relative to some prior call made by Hibernate to the ResultSet).

The example on page 206 is the following:

if ( resultSet.wasNull() ) {
return null;
}
BigDecimal value = resultSet.getString( names[0] );


The right implementation should be something like this:

BigDecimal value = resultSet.getString( names[0] );
if ( resultSet.wasNull() ) {
return null;
}

or

BigDecimal value = resultSet.getString( names[0] );
if ( value !=null && resultSet.wasNull() ) {
return null;
}

(not sure if that second situation will ever occurr though).

If I'm correct, I think it would be important to fix that sample in the book and also explain the meaning of the call to wasNull() (I gotta admit that I bindly copied the sample without analyzing what should call meant smilie.

Any thoughts?

-- Felipe
simonmartin (1) [Avatar] Offline
#2
Re: Bug on UserType samples?
You are definitely correct!

I spent far too long trying to fathom out why i was getting a null value for my custom type all of a sudden.

I realised that by moving my properties in the mapping file around for a particular class, I was getting different results (which is totally barmy and didn't make sense). Tracked it down to the same thing as you - because the previous property suddenly had a null value then so did my custom type.

I guess we should have looked at the code when we typed it in, instead of blindly accepting what was in the book smilie

Cheers,

Simon
gduan2000 (1) [Avatar] Offline
#3
Re: Bug on UserType samples?
felipeal,

Thanks for posting this message.

g