Question

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?

Was it helpful?

Solution

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

ConvertUtils.register(new LongConverter(null), Long.class); 

OTHER TIPS

In web.xml use below code

<init-param>
      <param-name>convertNull</param-name>
      <param-value>true</param-value>
</init-param>

So this converts the default 0 to null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top