Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
620 views
in Technique[技术] by (71.8m points)

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to specify the @Type(type = "uuid-char") in addition to @Column(name="...", columnDefinition = "uniqueidentifier"), 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...