views:

744

answers:

1

I have a numeric database field (numeric(3) in SQL Server 2000) that allows nulls, and null is my preferred "no value" value.

That field is mapped to the non-primitive java Long class in Hibernate:

<property column="my_column" name="my_column" type="java.lang.Long" not-null="false" />

The field is set as java.lang.Long in both my struts form and bean. When the Struts form is first created, I've verified that the property returns null. When the bean is created (pulled from the database), I've verified that the property returns null. However, after using BeanUtils.copyProperties() to prefill the Struts form with the bean values, the property then returns 0, and if I continue and save the form the database will have the 0 value.

Am I using the wrong types or combination of types (in database and/or Hibernate/Java) to maintain a null value for a numeric field? Should I map the numeric sql type to a different Java class like BigDecimal? In researching, I've found mention of a Converter class. Do I need to create such a class to help BeanUtils properly maintain null values?

+2  A: 

You need to register a LongConverter with null for a default value in BeanUtils.

BeanUtils.register(new LongConverter(null), Long.class);
stevedbrown
Thanks. I added `ConvertUtils.register(new LongConverter(null), Long.class);` just before the copy properties call and that fixed it. However, is that the best place to register the Long converter? From what I've read, converter registration is global, so should this be registered elsewhere and, if so, where?
Nicole
I moved the registration call to an initialization servlet for the application so it now works globally. Thanks for your help.
Nicole
Sounds like the right place to do it, sorry for not replying.
stevedbrown