Insert a row with Instant-DATETIME column to MySQL using JOOQ

huangapple 未分类评论47阅读模式
英文:

Insert a row with Instant-DATETIME column to MySQL using JOOQ

问题

我试图使用JOOQ将一行插入到MySQL 8中,其中Instant <--> DATETIME映射,但我得到了org.jooq.exception.DataAccessException,根本原因是com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect datetime value: '2020-05-02 02:45:38.134463+00:00' for column 'created' at row 1

由于某种原因,JOOQ在DATETIME列中添加了+00:00,而MySQL不支持该格式。

任何关于JOOQ是否可以直接处理此问题的想法吗?

  1. CREATE TABLE `user` (
  2. `id` BIGINT NOT NULL AUTO_INCREMENT,
  3. `version` BIGINT NOT NULL DEFAULT 0,
  4. `name` VARCHAR(255) NOT NULL,
  5. `created` DATETIME NOT NULL,
  6. PRIMARY KEY (`id`),
  7. UNIQUE INDEX `ind_name`(`name`)
  8. );
  1. @Getter
  2. @Setter
  3. public class User {
  4. @NonNull
  5. @Id
  6. private Long id;
  7. @NonNull
  8. @Version
  9. private Long version;
  10. @NonNull
  11. @Column(name = "name")
  12. private String name;
  13. @NonNull
  14. @Column(name = "created")
  15. private Instant created;
  16. }
  1. dslContext.insertInto( DSL.table( "user" ), field( "name", String.class ),
  2. //or field( "created", Instant.class), the result is the same
  3. field( "created", SQLDataType.INSTANT ) )
  4. .values( name, Instant.now() )
  5. .returningResult( DSL.asterisk() )
  6. .fetchOne()
  7. .into( User.class );
英文:

I'm trying to use JOOQ to insert a row to MySQL 8 with Instant <--> DATETIME mapping, but I'm getting org.jooq.exception.DataAccessException with root cause com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect datetime value: &#39;2020-05-02 02:45:38.134463+00:00&#39; for column &#39;created&#39; at row 1

For some reason JOOQ adds +00:00 to DATETIME column and that format is not supported by MySQL.

Any ideas if JOOQ can manage that out-of-the-box?

  1. CREATE TABLE `user` (
  2. `id` BIGINT NOT NULL AUTO_INCREMENT,
  3. `version` BIGINT NOT NULL DEFAULT 0,
  4. `name` VARCHAR(255) NOT NULL,
  5. `created` DATETIME NOT NULL,
  6. PRIMARY KEY (`id`),
  7. UNIQUE INDEX `ind_name`(`name`)
  8. );
  1. @Getter
  2. @Setter
  3. public class User {
  4. @NonNull
  5. @Id
  6. private Long id;
  7. @NonNull
  8. @Version
  9. private Long version;
  10. @NonNull
  11. @Column(name = &quot;name&quot;)
  12. private String name;
  13. @NonNull
  14. @Column(name = &quot;created&quot;)
  15. private Instant created;
  16. }
  1. dslContext.insertInto( DSL.table( &quot;user&quot; ), field( &quot;name&quot;, String.class ),
  2. //or field( &quot;created&quot;, Instant.class), the result is the same
  3. field( &quot;created&quot;, SQLDataType.INSTANT ) )
  4. .values( name, Instant.now() )
  5. .returningResult( DSL.asterisk() )
  6. .fetchOne()
  7. .into( User.class );

huangapple
  • 本文由 发表于 2020年5月2日 12:21:17
  • 转载请务必保留本文链接:https://java.coder-hub.com/61554418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定