jOOQ / jOOU

jOOU - Unsigned Integers jOOU provides unsigned integer versions for the four Java integer types byte, short, int and long.
http://www.jooq.org/products
Apache License 2.0
228 stars 32 forks source link

Storing UByte[] in mysql #19

Closed amrnablus closed 5 years ago

amrnablus commented 5 years ago

Expected behavior and actual behavior:

I get an error saying: Incorrect string value: '\x00\x00...' for column ...

Steps to reproduce the problem:

Create a JPA entity with a field of type UByte[] and persist it

Versions:

lukaseder commented 5 years ago

Thank you very much for your message.

jOOU is just a library providing some data types. It has nothing to do with JPA. In order to make JPA support jOOU types, you will need to provide vendor-specific type mappings (e.g. UserType in Hibernate). This isn't something jOOU would want to offer.

amrnablus commented 5 years ago

Thanks Luka, i ended up using a convertor. I will post the details here in case someone runs into the same issue: In the JPA Entity:

@Convert(converter = UBytesListConverter.class)
private UByte[] data;

The convertor class:


@Converter
public class UBytesListConverter implements AttributeConverter<UByte[], byte[]> {

    @Override
    public byte[] convertToDatabaseColumn(UByte[] bytesList) {
        byte[] res = new byte[bytesList.length];
        for( int i=0; i<bytesList.length; i++ ) {
            res[i] = bytesList[i].byteValue();
        }

        return res;
    }

    @Override
    public UByte[] convertToEntityAttribute(byte[] entity) {
        UByte[] res = new UByte[entity.length];
        for( int i=0; i<entity.length; i++ ) {
            res[i] = ubyte(entity[i]);
        }

        return res;

    }
}
lukaseder commented 5 years ago

Ah yes, of course. That's a viable option as well. Thanks for documenting this here.

Do note, however, that a UByte[] might take significantly more memory in your Java client, than a byte[]

amrnablus commented 5 years ago

Yeah we gotta pay for quality my friend haha :P