Java Hibernate和SQL Server中UUID的不同表示 [英] Different representation of UUID in Java Hibernate and SQL Server

查看:315
本文介绍了Java Hibernate和SQL Server中UUID的不同表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Hibernate将POJO中的 UUID 列映射到SQL Server表列。

注释应用如下:

  @Id 
@GeneratedValue
@Column(name =Id ,columnDefinition =uniqueidentifier)
public UUID getId(){...}

但是,似乎Java Hibernate映射和SQL服务器之间存在一些字节顺序问题。



例如,在我的Java应用程序中,我将id表示为:

  4375CF8E-DEF5-43F6-92F3-074D34A4CE35 
ADE3DAF8-A62B-4CE2-9D8C-B4E4A54E3DA1

而在SQL Server中,它们表示为:

  8ECF7543-F5DE-F643-92F3-074D34A4CE35 
F8DAE3AD-2BA6-E24C-9D8C-B4E4A54E3DA1

有什么方法可以在双方都有相同的表示方式吗?



请注意 uniqueidentifier 仅用于拥有一个 uniqueidentifier 在SQL服务器中键入id而不是类型 binary ;从注释中移除 uniqueidentifier 时,存在相同的问题(通过将 binary> 转换为 uniqueidentifier )。

解决方案

您需要指定 Type(type =uuid-char),另请参阅在JPA / hibernate中映射UUID的问题。另外,您可以使用字符串字段作为id在Java中仍然保留SQL Server中的 uniqueidentifier


I am trying to map a UUID column in POJO to SQL Server table column using Hibernate.

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.

For example, in my Java app, I have ids represented as:

4375CF8E-DEF5-43F6-92F3-074D34A4CE35
ADE3DAF8-A62B-4CE2-9D8C-B4E4A54E3DA1

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?

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).

解决方案

You need to specify the @Type(type = "uuid-char"), see also Problems mapping UUID in JPA/hibernate .

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆