Hibernate的uuid生成和mysql的uuid函数uuid() [英] Hibernate uuid generation and mysql uuid function uuid()
问题描述
我使用二进制格式将UUID存储在mysql数据库中(binnary(16))
我在hibernate中使用下面的代码
$ $ $ $ $ $ $ $ b $ @ $ $ $ $ $ @ @GeneratedValue(generator =uuid)
@GenericGenerator(name =uuid,strategy = uuid2)
@Column(columnDefinition =BINARY(16),length = 16)
private UUID id;
当我在phpmyadmin中查看时,这些生成的值看起来不同并且看起来像垃圾值。
$ b
如果我在mysql中使用UUID()函数查询它似乎很好。
然后如何告诉hibernate使用UUID()函数
什么是BINARY类型?
它们包含字节串而不是字符串。这意味着它们没有字符集,并且排序和比较基于值中字节的数值。这就是为什么它们看起来像垃圾值的原因
UUID()函数创建了什么?
UUID被设计成一个在时间和空间上全球唯一的数字。对UUID()的两次调用预计会生成两个不同的值,即使这些调用是在两个不相互连接的单独计算机上执行的.UUID是128位数字,由UTF8字符串表示,其中包含五个十六进制数字aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee格式:这就是为什么它们是可读的
UUID()
function doesn' t创建一个字节字符串,但是一个字符串。所以,他们是非常不同的。
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
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 .
这篇关于Hibernate的uuid生成和mysql的uuid函数uuid()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!