POST QUESTIONS ON THE FORUM! COMMENTS HERE SHOULD ADD VALUE TO THE
PAGE!On 27 Sep 2005 19:21, phersh wrote:
>Hi,
>The nullSafeGet doesn't protect from nulls because
>String value = resultSet.getString(names[0]); can return null.
>I've changed nullSafeGet to:
> public Object nullSafeGet(ResultSet resultSet, String[] names,
>Object owner)
> throws HibernateException, SQLException {
> if (resultSet.wasNull()) return null;
> String value = resultSet.getString(names[0]);
> if (value == null) return null;
> return getObject(value);
> }
I ran into a very subtle bug with this method. Depending on the
ordering of the property definitions in my class mapping the call to
wasNull() would cause a SQLException. I changed the method like this:
public Object nullSafeGet(ResultSet resultSet, String[] names,
Object owner) throws HibernateException, SQLException {
String value = resultSet.getString(names[0]);
if (resultSet.wasNull())
return null;
return getObject(value);
Maybe this isn't so good if the column type is not 'string' compatible,
but it seems that is the only possibility for this class.
Here's the mapping that failed:
...
<class name="org.egcrc.cfr.hibernate.EnumTestClass" table="ENUM_TEST"
lazy="true">
<id name="id" column="ID" type="long" unsaved-value="null">
<generator class="native"/>
</id>
<property name="alone" type="org.egcrc.cfr.hibernate.EnumUserType"
column="ALONE"/>
<property name="foo" type="string" column="FOO"/>
<property name="internal" type="org.egcrc.cfr.hibernate.EnumUserType"
column="INTERNAL"/>
</class>
...
The class has three instance variables, two of which are Java 1.5 enums,
and one is string.
But if I change this mapping to put the foo property either before the
two enums or after them it works with the original code. If you look at
the javadoc for ResultSet#wasNull() it states that you must have called
a column getter before calling it. Perhaps that was not done? I didn't
have the time to trace through the code in that much detail, but my
change did fix the problem.
I hope this helps someone avoid this problem. |