0

I am storing UUIDs as a primary key.

  @GeneratedValue(generator = "UUID_GENERATOR")
    @GenericGenerator(name = "UUID_GENERATOR", strategy = "uuid2")
    @Column(name = "store_id")
    private UUID storeId;

The problem with this is in sql_server the UUIDs are storing a different order compared to what I got from the JSON output.

JSON output

 "storeId": "3854fe95-e6b6-4319-9d9a-bfe67c9a07d6",

Database

storeID: "95FE5438-B6E6-1943-9D9A-BFE67C9A07D6"

The initial 8-4-4 digits are storing in a different order whereas last 4-12 digits are same. What is the reason for this issue and how to solve this?

ark
  • 3,707
  • 5
  • 21
  • 36
  • JSON output *from where*? How did you derive the JSON output, how did you store the data in SQL Server, and how are you retrieving the data from SQL Server? – Aaron Bertrand Nov 07 '18 at 20:52
  • Maybe the column is set to auto gen the uid. – Jacob H Nov 07 '18 at 20:52
  • @AaronBertrand from the endpoint that I used to post the data to the db. – ark Nov 07 '18 at 20:55
  • @JacobH I didn't get it – ark Nov 07 '18 at 20:56
  • That's not very specific. I don't know what your endpoint is and I don't even know which of my questions you're answering there. Best of luck. /shrug – Aaron Bertrand Nov 07 '18 at 20:56
  • Possible duplicate of [Different representation of UUID in Java Hibernate and SQL Server](https://stackoverflow.com/questions/41651681/different-representation-of-uuid-in-java-hibernate-and-sql-server) – GreyOrGray Nov 07 '18 at 22:41

2 Answers2

2

The difference is endianness; Microsoft likes to store the first half as little endian, which is a bit odd because they always store the second half as big endian. Everyone else stores the entire thing as big endian, which makes things much simpler and more portable.

StephenS
  • 1,813
  • 13
  • 19
0

You are probably using Hibernate then this code should work:

@Id
@GeneratedValue
@Type(type = "uuid-char")
@Column(columnDefinition="uniqueidentifier")
protected UUID id;