2
votes

I want to use UUIDs as primary due to the fact that those primaries are globally unique which makes it (for example) easy to integrate data from a production environment into a running debug environment.

Regarding to the following article: http://iops.io/blog/storing-billions-uuid-fields-mysql-innodb SELECT/INSERT of millions of records into a table using a UUID decoded as BINARY(16) is significantly faster than using simple CHAR(36).

Now, using Hibernate @GenericGenerator annotation, I could use this native UUID generator for a primary key using UUIDs:

@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;

On the other hand I could define a binary UUID as primary as follows:

@Id
@Getter
@Column(columnDefinition = "BINARY(16)", length = 16, updatable=false, nullable=false)
private byte[] id;

... and use @PrePersist to generate new UUIDs

@PrePersist
private void prePersist() {
    if (this.id == null) {
        this.generateUUID();
    }
}

The problem with this solution is the representation as binary for filters within (native/named) queries:

SELECT * from object o WHERE o.id=:id

What I would really need is the ability store the UUID field as above-mentioned as BINARY within the database while representing the value as simple UUID-String.

Is there any way to do that? Is there any alternative?

1

1 Answers

0
votes

Why don't you just use the special uuid type for the column?

    @Type(type = "pg-uuid")

But I also have the problem with native queries when doing like that.