Cannot add UUID of type Binary (16) into SQL

浪尽此生 提交于 2019-12-25 01:45:42

问题


By following exactly this question and the solution as well, I still cannot solve the problem. I still get " Data too long for column 'id' at row 1"

  public static byte[] asBytes(UUID uuid) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());
    return bb.array();
  }

  UUID id= UUID.randomUUID();
  InsertQueryBuilder runner = new InsertQueryBuilder("test_table")
        .addField("id", asBytes(id));

       return getConnection()
      .flatMap(connection -> runner.run(connection)
        .doOnTerminate(connection::close))

From that answer I understood that 16 bytes is 32 hex digits. So If I change column type to BINARY (32), then I Don't get any error and id gets successfully written in DB.

I tried to keep the ByteBuffer bb size to 8, but then I get;

BufferOverflowException

What am I missing?

来源:https://stackoverflow.com/questions/50452958/cannot-add-uuid-of-type-binary-16-into-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!