New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.time.Instant support #485

Closed
EAnushan opened this Issue Feb 2, 2018 · 6 comments

Comments

Projects
None yet
2 participants
@EAnushan

EAnushan commented Feb 2, 2018

When using the Spring JDBC implementation, if using the CRUD interface, Instant is handled properly:

jdbcTemplateCrud =
        JdbcTemplateMapperFactory.newInstance()
            .crud(modelClass, idClass)
            .lazilyTo(jdbcTemplate.getJdbcOperations(), tableName);

jdbcTemplateCrud.create(entity, entity::setId);

But, when using a standard Spring JDBC Template query and the provided sql parameter source:

sqlParameterSourceFactory =
        JdbcTemplateMapperFactory.newInstance().newSqlParameterSourceFactory(modelClass);

KeyHolder keyHolder = new GeneratedKeyHolder();

jdbcTemplate.update(
      insert(dataColumnNames),
      sqlParameterSourceFactory.newSqlParameterSource(entity),
      keyHolder);

I get the following exception from MySQL:

com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect datetime value: '\xAC\xED\x00\x05sr\x00\x0Djava.time.Ser\x95]\x84\xBA\x1B"H\xB2\x0C\x00\x00xpw\x0D\x02\x00\x00\x00\x00Zs\xABC+\xDEx\x00x' for column 'last_modified_date' at row 1

Do I need to do anything special to map Instant type parameters when not using the CRUD?

As I understand it, the included SqlParameterSourceImpl is more performant than Spring JDBC's BeanPropertySqlParameterSource as noted http://simpleflatmapper.org/0104-getting-started-springjdbc.html. I originally had an extended version of BeanPropertySqlParameterSource that handled Instant and Enum, will I need to do the same for SqlParameterSourceImpl or can I leverage the handling that the CRUD interface already includes?

@EAnushan

This comment has been minimized.

Show comment
Hide comment
@EAnushan

EAnushan Feb 2, 2018

I tried setting manually specifying the SqlType of my Instant columns as follows:

JdbcTemplateMapperFactory jdbcTemplateMapperFactory = JdbcTemplateMapperFactory.newInstance();
      jdbcTemplateMapperFactory.addColumnProperty(
          "created_date", SqlTypeColumnProperty.of(Types.TIMESTAMP));
      jdbcTemplateMapperFactory.addColumnProperty(
          "last_modified_date", SqlTypeColumnProperty.of(Types.TIMESTAMP));

SqlParameterSource sqlParameterSource = jdbcTemplateMapperFactory
      .newSqlParameterSourceFactory(modelClass)
      .newSqlParameterSource(entity);

System.out.println(sqlParameterSource.getSqlType("created_date"));

It doesn't seem to propagate through to the resultant SqlParameterSource. The SqlType on the last line is marked as UNKNOWN.

EAnushan commented Feb 2, 2018

I tried setting manually specifying the SqlType of my Instant columns as follows:

JdbcTemplateMapperFactory jdbcTemplateMapperFactory = JdbcTemplateMapperFactory.newInstance();
      jdbcTemplateMapperFactory.addColumnProperty(
          "created_date", SqlTypeColumnProperty.of(Types.TIMESTAMP));
      jdbcTemplateMapperFactory.addColumnProperty(
          "last_modified_date", SqlTypeColumnProperty.of(Types.TIMESTAMP));

SqlParameterSource sqlParameterSource = jdbcTemplateMapperFactory
      .newSqlParameterSourceFactory(modelClass)
      .newSqlParameterSource(entity);

System.out.println(sqlParameterSource.getSqlType("created_date"));

It doesn't seem to propagate through to the resultant SqlParameterSource. The SqlType on the last line is marked as UNKNOWN.

@arnaudroger

This comment has been minimized.

Show comment
Hide comment
@arnaudroger

arnaudroger Feb 2, 2018

Owner

Thanks for the big report will check asap

Owner

arnaudroger commented Feb 2, 2018

Thanks for the big report will check asap

@arnaudroger

This comment has been minimized.

Show comment
Hide comment
@arnaudroger

arnaudroger Feb 2, 2018

Owner

so I'm using spring javaTypeToSqlParameterType to get the sqlType from the java class and it does not recognise Instant.
also it does not get into account SqlTypeColumnProperty which it should will fix all that.
should be able to release that soon

Owner

arnaudroger commented Feb 2, 2018

so I'm using spring javaTypeToSqlParameterType to get the sqlType from the java class and it does not recognise Instant.
also it does not get into account SqlTypeColumnProperty which it should will fix all that.
should be able to release that soon

@arnaudroger arnaudroger added this to the 3.15.5 milestone Feb 2, 2018

@arnaudroger

This comment has been minimized.

Show comment
Hide comment
@arnaudroger

arnaudroger Feb 2, 2018

Owner

got a fix for instant also added conversion, as I had assumed that spring would deal with it, but does not really. will be able to release it tonight GMT

Owner

arnaudroger commented Feb 2, 2018

got a fix for instant also added conversion, as I had assumed that spring would deal with it, but does not really. will be able to release it tonight GMT

@EAnushan

This comment has been minimized.

Show comment
Hide comment
@EAnushan

EAnushan Feb 2, 2018

Thanks @arnaudroger for looking into it. I'll give it a shot after it's released.

FYI, Spring might not handle Enums properly either? I haven't tried this, but from my experience with plain Spring JDBC, I had to add special handling for both Instant and Enum types.

EAnushan commented Feb 2, 2018

Thanks @arnaudroger for looking into it. I'll give it a shot after it's released.

FYI, Spring might not handle Enums properly either? I haven't tried this, but from my experience with plain Spring JDBC, I had to add special handling for both Instant and Enum types.

@arnaudroger

This comment has been minimized.

Show comment
Hide comment
@arnaudroger

arnaudroger Feb 2, 2018

Owner

Just put 3.15.5 to maven central, should be available in the next 30mn.

The converter should take care of the Enum, you might need to specify the sqltype though, so it can choose the ordinal or string version.

Owner

arnaudroger commented Feb 2, 2018

Just put 3.15.5 to maven central, should be available in the next 30mn.

The converter should take care of the Enum, you might need to specify the sqltype though, so it can choose the ordinal or string version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment