0

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?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Anum Sheraz
  • 2,383
  • 1
  • 29
  • 54
  • 1
    What's the question? 16 bytes are BINARY(16) and 8 bytes are not enough for two `long` – Adriano Repetti May 21 '18 at 16:29
  • If 16 bytes are binary(16), then why I the SQL complains "Data too long for column 'id' at row 1" ? – Anum Sheraz May 21 '18 at 16:30
  • Okay I think I understood from your last comment now. I don't need to put another long of 8 bytes. Query executes successfully after removing ` bb.putLong(uuid.getLeastSignificantBits());` and keeping the byte length 8. – Anum Sheraz May 21 '18 at 16:36
  • Can a ByteBuffer be directly used as a parameter; or should you be getting the underlying array? – Uueerdo May 21 '18 at 16:40
  • If I convert UUID to bytes of length 8, and then write it to SQL column of BINARY(16), I am left with remaining zeros e.g. 0x34676358617562000000000000000000. What I don't understand is WHY the SQL complains "DATA too long" , If the convert UUID to byte(16) ! are there any HIDDEN characters ? – Anum Sheraz May 21 '18 at 16:45
  • But 8 bytes UUID isn't likely to be really unique, don't drop least significants bytes – Adriano Repetti May 21 '18 at 16:48
  • Why not store the UUID as human-readable text? A [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)) is a 128-bit value, which can *theoretically* fit in 16 bytes, but representing them as a 36-character string is more typical. The additional space required is largely a non-issue unless you're dealing with trillions of records, but the benefits to readability are huge. – tadman May 21 '18 at 17:01

0 Answers0