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.

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