Java Hibernate 和 SQL Server 中 UUID 的不同表示 [英] Different representation of UUID in Java Hibernate and SQL Server
问题描述
我正在尝试使用 Hibernate 将 POJO 中的 UUID
列映射到 SQL Server 表列.
I am trying to map a UUID
column in POJO to SQL Server table column using Hibernate.
注释应用如下:
@Id
@GeneratedValue
@Column(name = "Id", columnDefinition = "uniqueidentifier")
public UUID getId(){ ... }
但是,Java Hibernate 映射和 SQL 服务器之间似乎存在一些字节序问题.
However, it seems that there is some endianness problem between the Java Hibernate mapping and SQL server.
例如,在我的 Java 应用程序中,我的 ID 表示为:
For example, in my Java app, I have ids represented as:
4375CF8E-DEF5-43F6-92F3-074D34A4CE35
ADE3DAF8-A62B-4CE2-9D8C-B4E4A54E3DA1
而在 SQL Server 中,这些表示为:
whereas in SQL Server, these are represented as:
8ECF7543-F5DE-F643-92F3-074D34A4CE35
F8DAE3AD-2BA6-E24C-9D8C-B4E4A54E3DA1
有没有办法让双方都拥有相同的代表?
Is there any way to have same representation at both sides?
请注意,uniqueidentifier
仅用于在 SQL Server 中具有 uniqueidentifier
类型的 id,而不是 binary
类型;当uniqueidentifier
从注解中移除时,同样的问题存在(这个问题可以通过将binary
转换为uniqueidentifier
来观察).
Please note that uniqueidentifier
is used only to have a uniqueidentifier
typed id in SQL server instead of type binary
; the same problem exists when uniqueidentifier
is removed from annotation (the problem can be observed by converting binary
is to uniqueidentifier
).
推荐答案
除了 @Column(name="...", columnDefinition = "uniqueidentifier")
,另见 在 JPA/hibernate 中映射 UUID 的问题 .
或者,您可以在 Java 中使用 String
字段作为 id,而在 SQL Server 中仍然保留 uniqueidentifier
.
Alternatively you can use a String
field for the id in Java and still keep uniqueidentifier
in SQL Server.
这篇关于Java Hibernate 和 SQL Server 中 UUID 的不同表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!