Skip to content

Add support for conversion to JdbcValue and store byte[] as binary [DATAJDBC-327] #549

Closed
@spring-projects-issues

Description

@spring-projects-issues

mikleing opened DATAJDBC-327 and commented

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:

@WritingConverter@WritingConverter
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


Affects: 1.0.4 (Lovelace SR4)

Referenced from: pull request #123

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions