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.

Data JDBC don't handle UUID stored as BINARY(16) in MySQL

See original GitHub issue

Task List

  • Steps to reproduce provided
  • Stacktrace (if present) provided
  • Example that reproduces the problem uploaded to Github
  • Full description of the issue provided (see below)

Hello!

I’m trying to migrate my application from JPA to JDBC in order to have more control of the queries and avoid the 1+N from the beginning. I found a problem regarding UUID column (not as id, just a regular column) that micronaut is unable to query this column (UUID -> VARBINARY(16)) and to convert back the response from VARBINARY(16) to UUID. For Micronaut Data JPA this works just fine.

This is the table I’m using to store some rows with UUID and to query them on a MySQL 8 database:

CREATE TABLE establishment (
  id   INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  uuid BINARY(16) NOT NULL,
  name VARCHAR(50) NOT NULL
);

This is the entity:

@Getter
@Setter
@MappedEntity
public class Establishment {

    @Id
    @GeneratedValue
    private Integer id;

    private UUID uuid;
    private String name;
}

The repository:

@JdbcRepository(dialect = Dialect.MYSQL)
public interface EstablishmentRepository extends PageableRepository<Establishment, Integer> {

    Optional<Establishment> findByUuid(UUID uuid);
}

Ok, now the use cases:

1 - If I run establishmentRepository.findByUuid(/*some valid uuid*/), nothing is found in the query, even tho the row exists in the database with that uuid. Micronaut Data on TRACE log even shows the UUID on the binding list, but the query returns nothing.

2 - If I run establishmentRepository.findById(1), then the row is found but it throws an error when trying to convert from binary to uuid, something like this:

Cannot convert type [class java.lang.String] with value [�;$jNƑZ��] to target type: UUID uuid. Consider defining a TypeConverter bean to handle this case.

I took the suggestion and tried to create a few TypeConverters, but without success.

MySQL suggests the use of BINARY(16) to store UUIDs, and it works just fine in JPA. Is this feature missing in JDBC? I can help with some PR, just need some guidance on where to start looking to fix this UUID conversion.

Environment Information

  • Operating System: Linux
  • Micronaut Version: 2.2.0
  • JDK Version: 11

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:23 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
dstepanovcommented, Sep 7, 2021
2reactions
dstepanovcommented, May 11, 2021

@athkalia Try something like this:

public class DataInitializer2 implements ApplicationEventListener<ApplicationStartupEvent> {
    @Override
    public void onApplicationEvent(ApplicationStartupEvent event) {
        ConversionService.SHARED.addConverter(...)
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

mysql - Storing Java UUID's in binary database columns ...
The answer is available in Mysql Documentation for Binary and Varbinary. The BINARY and VARBINARY types are similar to CHAR and VARCHAR, ...
Read more >
How To Use UUIDs With Hibernate And MySQL
Instead, we should use a BINARY(16) column. This minimizes the required value size (less bytes, no dashes) and index size.
Read more >
Storing Java UUID's in binary database columns ...
I have long been accustoming to storing UUID values in binary database columns (e.g. BYTEA for PostgreSQL, BINARY(16) for MySQL).
Read more >
Storing UUID Values in MySQL Tables
If the UUID has to be a primary key, the gain is even greater, as in InnoDB the primary key value is copied...
Read more >
MySQL UUID() behavior
as i said,.uuid is not random, and can produces identical uuids in following rows, because the computers are today faster so that the...
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