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
Java

Avatar of undefined
Last Comment
GlobalDictator

8/22/2022 - Mon
CEHJ

Please post the error
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..
CEHJ

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."
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
for_yan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
GlobalDictator

ASKER
@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
CEHJ

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

ASKER
@CEHJ

Sorry for the late reply. I didnt ignore your comment but Yan's response was much more helpful due to the actual example.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.