1

I have something like that:
@Select("select * from table;") List<Hashmap<String, Object>> getAll()
However, from this map there are removed column with null value.
How to disable this removing and allowing for nulls?

  • 1
    [Question asked and answered](http://stackoverflow.com/q/31160046/7076047). Null Columns are not removed, they are just not set. the solutions are setting _callSettersOnNulls_ setting to true, or use _type handler_, or fetch result into custom type instead of map. – blackwizard Nov 17 '16 at 10:08
  • " the solutions are setting callSettersOnNulls " How to do it ? –  Nov 17 '16 at 10:56
  • I would like to get in hashmap (String -> Object). ("columnName" -> null), however I would **not** like to get ("columnName" -> 0) when columneName is type of integer –  Nov 17 '16 at 11:06
  • 1
    In Mybatis main config file: ``. But take the time to read on the subject, because of potential side effects since this is a global setting. – blackwizard Nov 17 '16 at 11:26
  • 1
    The BaseTypeHandler overrrides default JDBC NULL values handling; a null Integer column will be mapped to null java and not 0: `if (rs.wasNull()) { return null; } else { return result; }` – blackwizard Nov 17 '16 at 11:29
  • @blackwizard what about configuration without xml ? Only annotations. Can you provide mo some link where I can read about it. –  Nov 17 '16 at 15:30
  • 1
    This setting goes to main configuration file (the one where you define datasource amongst all things), not in mappers. A link? http://google.com , http://stackoverflow.com/ I just recommend you to read about issues other users have encountered because this is a global setting. – blackwizard Nov 17 '16 at 15:46
  • @blackwizard, I have no configuration file - I use only annotations. Do you know proper annotation ? –  Nov 17 '16 at 15:49
  • then how do configure the SqlSessionFactory with annotation? because I have never seen that before (I might learn something) – blackwizard Nov 17 '16 at 16:26
  • alright, I guess you are using something like _Spring-boot_. The setting is part of _Configuration_ bean used by the _sqlSessionFactory bean_. Or set it via Java API: `sessionFactory.getConfiguration().setCallSettersOnNulls(true);` – blackwizard Nov 17 '16 at 17:03
  • Thanks, Is it possible to do it using annotations ? –  Nov 17 '16 at 17:28
  • I have not found any way. Either because this not implemented (yet) or this is beyond the limits of what you can do with annotations. It seems you are not the only _"annotation fundamentalist"_, [here is another post on related subject](http://stackoverflow.com/questions/33652793/mybatis-spring-java-annotation-for-multiple-datasources/33957496) – blackwizard Nov 17 '16 at 20:21
  • Ok, I set it, but still no columns with nulls. What sshould I do? Keep in mind, that nulls fields are privmitive types (int, deouble) –  Nov 18 '16 at 09:59
  • debug: set break point in Mybatis classes in `IntegerTypeHandler.getNullableResult` , `BaseTypeHandler.getResult` to see was happens with the `rs.wasNull` test. – blackwizard Nov 18 '16 at 10:53
  • Where Can I find this calss ? –  Nov 18 '16 at 13:23
  • in package org.apache.ibatis.type; – blackwizard Nov 18 '16 at 13:27
  • wasNull is returning true –  Nov 18 '16 at 14:30
  • I dont understand some thing. Continuing debugging I come across: callSettersOnNulls is false, however, in my application file is: mybatis.configuration.callSettersOnNulls=true –  Nov 18 '16 at 14:34

1 Answers1

0

You've got a List of Hashmaps , maybe you can try it better only by using a HashMap. Elsewhere you can try to read out the values from the database manually by not using the @Select Command.

Arol
  • 62
  • 7