question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom type encoding (UUID)

See original GitHub issue

Hi,

I’m trying to insert a UUID in a DB but the save call fails saying that there are no encoders supporting this type. Indeed DefaultCodecs does not have a UUIDCodec. I was not able to find a way to add a custom Codec though. Did I miss something? How would you recommend I can fix my problem?

For context, my DAO looks like this:

class Person {
  @Id
  Long id;
  UUID userUuid;
  String name;
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
Tuegorcommented, Jul 26, 2022

Hi there, thanks for your suggestion.

The principle of R2DBC MySQL features’ provision is: “Compliant with R2DBC Specification, providing the same or compatible JDBC features.”

See https://github.com/r2dbc/r2dbc-spi/blob/master/r2dbc-spec/src/main/asciidoc/data-types.adoc , that means if MySQL does not support UUID, driver will be not provide UUID data type. (In fact, MySQL does not support UUID)

Good at evasions, huh? It that’s so, why does Spring Data JPA with a non-reactive driver support it no sweat? I’ll tell you why - because your statement is a lie. MySQL does support UUID, although it doesn’t have a separate data type for it like Postgres. That’s why it has the UUID() function, whose return value can be stored in a CHAR column. And if you apply UUID_TO_BIN() function to this value, you can store it compressed in a BINARY column. So say it straight - you are plain lazy to implement it, although it’s needed and two years have passed since this report.

2reactions
adrilocommented, Feb 12, 2020

I managed to achieve what I want by registering custom Converters:

public class MySQLConfiguration extends AbstractR2dbcConfiguration {
    @Override
    protected List<Object> getCustomConverters() {
        return List.of(new UUIDToByteArrayConverter(), new ByteArrayToUUIDConverter());
    }
    ...
}

@WritingConverter
public class UUIDToByteArrayConverter implements Converter<UUID, byte[]> {
    @Override
    public byte[] convert(UUID source) {
        ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
        bb.putLong(source.getMostSignificantBits());
        bb.putLong(source.getLeastSignificantBits());
        return bb.array();
    }
}

@ReadingConverter
public class ByteArrayToUUIDConverter implements Converter<byte[], UUID> {
    @Override
    public UUID convert(byte[] source) {
        return UUID.nameUUIDFromBytes(source);
    }
}

Still, given the large usage of UUIDs, it may be worth adding a codec for this type as part of the default set.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Avro custom decoding of UUID through kafka on consumer end
I've written a class to custom encode objects of UUID type to bytes to be transported across kafka and avro. To use this...
Read more >
Customizing how an external Swift type is encoded or decoded
Various ways to handle mismatches between how an external type is expected to be coded and the data format that an app is...
Read more >
Introduction — ramsey/uuid stable Manual
The most widely-known and used types of UUIDs are defined by RFC 4122. A UUID, when encoded in hexadecimal string format, looks like:....
Read more >
Message Repository Table Schema - EventSauce
The UUID Encoding package allows customizing how the UUIDs used for the event ID and the aggregate root ID are converted to string...
Read more >
Fixing issues with Codable identifiers | by Mason O'Neil
Generating the UUID inline will create a new unique identifier every time a new Employee object is instantiated. Perfect! However, the value ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found