Sagar
Sagar

Reputation: 997

Hibernate uuid generation and mysql uuid function uuid()

I'm using binary format to store UUIDs in mysql database ( binnary(16) )

and I'm useing the following code in hibernate

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

those generated values look different and seems as garbage values when I view in phpmyadmin

if I use UUID() function in mysql query it seems fine.
Then how to tell hibernate to use UUID() function

Upvotes: 1

Views: 2462

Answers (1)

Keerthivasan
Keerthivasan

Reputation: 12880

What is BINARY Type ?

They contain byte strings rather than character strings. This means that they have no character set, and sorting and comparison are based on the numeric values of the bytes in the values. That's why they look like garbage values

What does UUID() function create?

A UUID is designed as a number that is globally unique in space and time. Two calls to UUID() are expected to generate two different values, even if these calls are performed on two separate computers that are not connected to each other.A UUID is a 128-bit number represented by a utf8 string of five hexadecimal numbers in aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee format: That's why they are readable

UUID() function doesn't create a byte string, but a character string. so, they are very different .

Upvotes: 1

Related Questions