18

I came across a blog of using UUID with Hibernate and MySql. Now the problem is, whenever I take a look at the database the ID's will be non-readable format (binary-16). How can I store UUID as a readable format like 7feb24af-fc38-44de-bc38-04defc3804fe instead of ¡7ôáßEN¹º}ÅÑs

I was using this code

@Id
@GeneratedValue( generator = "uuid2" )
@GenericGenerator( name = "uuid2", strategy = "uuid2" )
@Column( name = "id", columnDefinition = "BINARY(16)" )
private UUID id;

And the result is ¡7ôáßEN¹º}ÅÑs. But I want it as readable UUID so I used the following code which didn't help me

@Id
@GeneratedValue( generator = "uuid2" )
@GenericGenerator( name = "uuid2", strategy = "uuid2" )
@Column( name = "id", columnDefinition = "CHAR(32)" )
private UUID id;

How to save the UUID as a string instead of binary(16) without changing the java type UUID

Sunil Rao
  • 800
  • 2
  • 6
  • 23
  • 1
    Think about this.. UUID in binary(16) is better for performance. – Raymond Nijland Apr 24 '18 at 14:15
  • You got result ¡7ôáßEN¹º}ÅÑs from console or any GUI tool? – dkb Apr 24 '18 at 14:17
  • check this: https://stackoverflow.com/questions/43394039/modeling-uuid-in-hibernate-entity-against-mysql – dkb Apr 24 '18 at 14:19
  • 6
    Why save it in an inefficient format? Just use `BIN_TO_UUID(id)` when you need to actually look at the table. – RealSkeptic Apr 24 '18 at 14:21
  • @dkb I copy pasted that value from mysql table. – Sunil Rao Apr 24 '18 at 14:24
  • @RaymondNijland yes I agree to that, but is there a way for just representing UUID in string in tables and use it as UUID object in java ? – Sunil Rao Apr 24 '18 at 14:24
  • `BIN_TO_UUID(id)` will only work in MySQL 8.0 @RealSkeptic – Raymond Nijland Apr 24 '18 at 14:27
  • 1
    "yes I agree to that, but is there a way for just representing UUID in string in tables and use it as UUID object in java ?" if you use MySQL 5.7+ you can use a generated column to generate a human readable UUID from the binary id column -> https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html .. or use a basic view if you don't have MySQL 5.7+ – Raymond Nijland Apr 24 '18 at 14:40

4 Answers4

50

just use @org.hibernate.annotations.Type(type="uuid-char")

There is three levels of data types:
- Java types
- Hibernate's types
- Database Specific types.

Hibernate data type presentation is a bridge between Java data type and Database types to be independent from database.

You can check this mappings. As you can find there java.util.UUID can be mapped to diferent types (binary or char/varchar). uuid-binary is key to hibernate's UUIDBinaryType, you get this type by default and it will be mapped to BINARY of your database.

If you want to get CHAR type under your UUID, you should explain to hibernate that you want his UUIDCharType. To do that you use uuid-char key and as you can check in JavaDoc of @Type annotation: Defines a Hibernate type mapping.. So, you use annotation to explain hibernate which bridge it should use.

Ivan Osipov
  • 726
  • 8
  • 6
5

The @Type annotation is deprecated and does not even exist in the last version of Spring Boot.

You need to use now @JdbcTypeCode

The correct way to do that is the following

@Column(columnDefinition = "VARCHAR(36)")
@JdbcTypeCode(SqlTypes.VARCHAR)
Arnaud DUBOIS
  • 51
  • 1
  • 2
4

I had the same problem in my last project (spring-boot:2.5.3 + mariaDB).
So I added the following annotation @Type(type = "org.hibernate.type.UUIDCharType") to fix it.

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;

import javax.persistence.*;
import java.util.UUID;
/** imports */

@Entity()
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    @Column(name = "uuid", columnDefinition = "char(36)")
    @Type(type = "org.hibernate.type.UUIDCharType")
    private UUID uuid;

   /** ... */
}

The purpose of the hibernate annotation @Type is to define the type of data stored in database and it's compatible with the previous line @Column(name = "uuid", columnDefinition = "char(36)")

While googling it, I noticed that mariaDB & MySQL have the same issue.

You can see here more examples in different use cases for org.hibernate.annotations.Type

Radouane FADEL
  • 300
  • 3
  • 9
0

i'm using this annotations:

@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2r")
@Column(name = "id", columnDefinition = "varchar(36)")
@Type(type = "uuid-char")
private UUID uuid;
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 04 '22 at 00:21