Different representation of UUID in Java Hibernate and SQL Server

后端 未结 3 1677
星月不相逢
星月不相逢 2020-12-03 21:26

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

The annotations are applied as follows:

@Id
@Genera         


        
相关标签:
3条回答
  • 2020-12-03 21:58

    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.

    0 讨论(0)
  • 2020-12-03 21:59

    With SQL Server you should use guid strategy for your generator:

    @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

    Java is using UUID generator in version 4 how can you see here:

    4375CF8E-DEF5-43F6-92F3-074D34A4CE35

    ADE3DAF8-A62B-4CE2-9D8C-B4E4A54E3DA1

    0 讨论(0)
  • 2020-12-03 22:04

    Microsoft databases use GUIDs. It is Microsoft's implementation of the UUID standard.

    This being said, you should use the guid generator.

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

    Also, have you set SQLServer2012Dialect? This also might solve some future issues.

    0 讨论(0)
提交回复
热议问题