[英]Different representation of UUID in Java Hibernate and SQL Server
I am trying to map a UUID
column in POJO to SQL Server table column using Hibernate.我正在尝试使用 Hibernate 将 POJO 中的UUID
列映射到 SQL Server 表列。
The annotations are applied as follows:注释的应用如下:
@Id
@GeneratedValue
@Column(name = "Id", columnDefinition = "uniqueidentifier")
public UUID getId(){ ... }
However, it seems that there is some endianness problem between the Java Hibernate mapping and SQL server.但是,Java Hibernate 映射和 SQL 服务器之间似乎存在一些字节序问题。
For example, in my Java app, I have ids represented as:例如,在我的 Java 应用程序中,我的 id 表示为:
4375CF8E-DEF5-43F6-92F3-074D34A4CE35
ADE3DAF8-A62B-4CE2-9D8C-B4E4A54E3DA1
whereas in SQL Server, these are represented as:而在 SQL Server 中,这些表示为:
8ECF7543-F5DE-F643-92F3-074D34A4CE35
F8DAE3AD-2BA6-E24C-9D8C-B4E4A54E3DA1
Is there any way to have same representation at both sides?有没有办法在双方都有相同的代表?
Please note that uniqueidentifier
is used only to have a uniqueidentifier
typed id in SQL server instead of type binary
;请注意, uniqueidentifier
仅用于在 SQL 服务器中具有类型为 id 的uniqueidentifier
而不是类型binary
; the same problem exists when uniqueidentifier
is removed from annotation (the problem can be observed by converting binary
is to uniqueidentifier
).从注释中删除uniqueidentifier
时存在同样的问题(可以通过将binary
转换为uniqueidentifier
来观察问题)。
You need to specify the @Type(type = "uuid-char")
in addition to @Column(name="...", columnDefinition = "uniqueidentifier")
, see also Problems mapping UUID in JPA/hibernate .除了@Column(name="...", columnDefinition = "uniqueidentifier")
,您还需要指定@Type(type = "uuid-char")
,另请参阅在 JPA/hibernate 中映射 UUID 的问题。
Alternatively you can use a String
field for the id in Java and still keep uniqueidentifier
in SQL Server.或者,您可以在 Java 中为 id 使用String
字段,并且在 SQL Server 中仍然保留uniqueidentifier
。
Microsoft databases use GUIDs. Microsoft 数据库使用 GUID。 It is Microsoft's implementation of the UUID standard.它是 Microsoft 对 UUID 标准的实现。
This being said, you should use the guid generator.话虽如此,您应该使用 guid 生成器。
@Id
@GenericGenerator(name = "generator", strategy = "guid", parameters = {})
@GeneratedValue(generator = "generator")
public String getId() {
return id;
}
guid
uses a database-generated GUID string on MS SQL Server and MySQL.guid
在 MS SQL Server 和 MySQL 上使用数据库生成的 GUID 字符串。
Also, have you set SQLServer2012Dialect?另外,您是否设置了 SQLServer2012Dialect? This also might solve some future issues.这也可能解决一些未来的问题。
With SQL Server you should use guid strategy for your generator:对于 SQL Server,您应该为生成器使用guid策略:
@GeneratedValue(generator = "my-uid")
@GenericGenerator(name = "my-uid", strategy = "guid")
@Id
private UUID uuid;
https://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/mapping.html https://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/mapping.html
Java is using UUID generator in version 4 how can you see here: Java 在第 4 版中使用 UUID 生成器,你怎么能在这里看到:
4375CF8E-DEF5- 4 3F6-92F3-074D34A4CE35 4375CF8E-DEF5- 4 3F6-92F3-074D34A4CE35
ADE3DAF8-A62B- 4 CE2-9D8C-B4E4A54E3DA1 ADE3DAF8-A62B- 4 CE2-9D8C-B4E4A54E3DA1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.