jdbcTemplate.query(String sql, RowMapper<?> rowMapper, Object[] args)  returns an error when a field is null

GlobalDictator
GlobalDictator used Ask the Experts™
on
Hi,

Further to my last question, I'm getting an error when one of the mapped fields is null.
List<DelConversation> delConversations = jdbcTemplate.query(
        		SELECT_ALL_CONVERSATIONS_SQL,
        		new Object[] {dateFrom, dateTo}, new BeanPropertyRowMapper(DelConversation.class));

Open in new window


Is there a way that I can make the RowMapper to accept or ignore nulls?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
Please post the error

Author

Commented:
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [null] to required type [long] for property 'leftdate'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [null] to required type [long] for property 'leftdate': PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] returned inappropriate value

Open in new window


The DB field 'LeftDate' is empty in the DB, so I suppose thats whats causing he problem. When that field is populated, then there are no issues..
Top Expert 2016

Commented:
From the API docs:

"For 'null' values read from the databasem, we will attempt to call the setter, but in the case of Java primitives, this causes a TypeMismatchException. This class can be configured (using the primitivesDefaultedForNullValue property) to trap this exception and use the primitives default value. Be aware that if you use the values from the generated bean to update the database the primitive value will have been set to the primitive's default value instead of null."
Awarded 2011
Awarded 2011
Commented:

I guess something like this should allow you get away from errors when there is null in database for your long value:
BeanPropertyRowMapper bprm = new BeanPropertyRowMapper(DelConversation.class);
bprm.setPrimitivesDefaultedForNullValue(true);
List<DelConversation> delConversations = jdbcTemplate.query(
        		SELECT_ALL_CONVERSATIONS_SQL,
        		new Object[] {dateFrom, dateTo}, bprm);

Open in new window

Author

Commented:
@Yan,

Thanks very much for the practical demonstration of how to use the 'primitivesDefaultedForNullValue' property.

I'll implement this and let you know of the outcome.

Thanks
Top Expert 2016

Commented:
GlobalDictator, can you tell me why you ignored my comment, which explained the problem?

Author

Commented:
@CEHJ

Sorry for the late reply. I didnt ignore your comment but Yan's response was much more helpful due to the actual example.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial