Link to home
Start Free TrialLog in
Avatar of GlobalDictator
GlobalDictator

asked on

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

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Please post the error
Avatar of GlobalDictator
GlobalDictator

ASKER

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..
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."
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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
GlobalDictator, can you tell me why you ignored my comment, which explained the problem?
@CEHJ

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