5

I am using JdbcTemplate to get result of an query. Some of the columns can have null value in the DB table. But I am getting conversion exception complaining null can not be convert to long type.

How I can tell spring to put null in variable if it find null in the column?

I have data type as Long type.

αƞjiβ
  • 3,056
  • 14
  • 58
  • 95
  • possible duplicate of [How do I in JDBC read a possibly null double value from resultSet?](http://stackoverflow.com/questions/1103460/how-do-i-in-jdbc-read-a-possibly-null-double-value-from-resultset) – Fodder Oct 06 '14 at 22:39
  • I am talking about using Spring JdbcTemplate. – αƞjiβ Oct 06 '14 at 22:40
  • Do you have a class that maps your rows to an object? Could you use a RowMapper and just use `ResultSet.getLong()` and do something similar to the linked question? A bit messy though. :( – Fodder Oct 06 '14 at 23:17
  • Is there anything I can do with BeanPropertyRowMapper itself? – αƞjiβ Oct 06 '14 at 23:58
  • 1
    I'm not that familiar with BeanPropertyRowMapper, and looking at the API, it seems you can only configure whether it [throws an exception from getting a null for primative values](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/BeanPropertyRowMapper.html#setPrimitivesDefaultedForNullValue-boolean-). Although it says it automatically maps Longs, are you sure you are using a `Long` and not a `long`? – Fodder Oct 07 '14 at 00:22
  • Yes it was long instead of Long. Change fixed the issue. Thanks – αƞjiβ Oct 07 '14 at 14:08

1 Answers1

6

Going to post this as an answer, in case someone else has the same issue and doesn't read the comments (and so this question comes up as having an answer in search results).

The issue was that long was being used instead of Long. As longs cannot be null, it couldn't convert any nulls found in the database.

Fodder
  • 564
  • 1
  • 10
  • 23