Uploaded image for project: 'Spring Data JDBC'
  1. Spring Data JDBC
  2. DATAJDBC-327

Add support for conversion to JdbcValue and store byte[] as binary

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Resolved
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: 1.0.4 (Lovelace SR4)
    • Fix Version/s: 1.1 M3 (Moore)
    • Component/s: None
    • Labels:
      None

      Description

      Java type byte[] is not being stored properly in the database.  I'm using MySQL on the default driver.  Here is the entity:

      public class User {
           @Id
           public Integer id;
           public byte[] value;
      }

      I save a user with the byte array value 0xFFFFFFFFFFFFFFFF.

      User user = new User();
      user.value = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff};
      user = userRepository.save(user);
      

      The actual value saved in the database is the following.

      0x2D31000000000000
      

      The database type is BINARY(8).  I believe (but am not sure) that Spring Data JDBC thinks that the column type in the database is INT.

      The interesting thing is that a converter from an object to a byte array works fine.  Here is a working example:

      @[email protected]
      public static class UuidToByteConverter implements Converter<UUID, byte[]> {
           @Override public byte[] convert(UUID source) {
                ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
                byteBuffer.putLong(source.getMostSignificantBits());
                byteBuffer.putLong(source.getLeastSignificantBits());
                return byteBuffer.array();
           }
      }
      

      I think that byte arrays should automatically be stored correctly in the database.  If not, it would be helpful to know how to use a converter to correct the problem.

        Attachments

          Activity

            People

            Assignee:
            schauder Jens Schauder
            Reporter:
            mikleing mikleing
            Last updater:
            Spring Issues Spring Issues
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: