【问题标题】:Using Hibernate UUIDGenerator via annotations通过注解使用 Hibernate UUIDGenerator
【发布时间】:2011-09-15 10:57:41
【问题描述】:

我正在使用我的 uuid,如下所示:

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid")
@Column(name = "uuid", unique = true)
private String uuid;

但我收到了智能休眠警告:

使用 org.hibernate.id.UUIDHexGenerator 不生成 IETF RFC 4122 兼容的 UUID 值;考虑使用 org.hibernate.id.UUIDGenerator 代替

所以我想切换到org.hibernate.id.UUIDGenerator,现在我的问题是我应该如何将它告诉 Hibernate 的生成器。我看到有人将它用作“hibernate-uuid” - 所以这是我尝试过的,但结果是否定的:

@Id
@GeneratedValue(generator = "hibernate-uuid")
@GenericGenerator(name = "hibernate-uuid", strategy = "hibernate-uuid")
@Column(name = "uuid", unique = true)
private String uuid;

【问题讨论】:

    标签: java hibernate annotations uuid


    【解决方案1】:

    应该是uuid2:

    ...
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    ...
    

    5.1.2.2.1. Various additional generators

    【讨论】:

    【解决方案2】:

    HibernateDoc 表示您可以使用以下内容:

    @Id
    @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")
    @Column(name = "uuid", unique = true)
    private String uuid;
    

    我希望您使用的是 Hibernate 3.5。

    【讨论】:

    • system-uuid 只是生成器的名称,见第 3 行。在第 2 行中引用了它。
    【解决方案3】:

    正如@natan 在评论中指出的那样,如果您使用的是 Hibernate 5,那么下面的代码就足够了:

    @Id 
    @GeneratedValue
    private java.util.UUID id;
    

    在 MySQL 中使用 BINARY(16) 类型定义 id 列,或者在其他 SQL 实现中使用它的等效类型。

    【讨论】:

    • 或者也可以添加@Type(type="uuid-char")注解使用VARCHAR(36)
    • 对于 postgres @Type(type="pg-uuid") :D
    【解决方案4】:

    试试……

    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(name = "uuid", columnDefinition = "BINARY(16)")
    public UUID getId()
    {
        return id;
    }
    
    public void setId(UUID i)
    {
        id = i;
    }
    

    注意“uuid2”而不是“uuid”。

    【讨论】:

      【解决方案5】:

      未知的 Id.generator: hibernate-uuid

      @Id
      @GeneratedValue(generator = "uuid")
      @GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDGenerator")
      @Column(name = "id", unique = true)
      public String getId() {
          return id;
      }
      
      public void setId(String id) {
          this.id = id;
      }
      

      【讨论】:

        【解决方案6】:

        这将使用 UUID v4 并且自动生成的 uuid 将像往常一样存储在列中 varchar(36):

        @Id
        @GeneratedValue(generator = "uuid2")
        @GenericGenerator(name = "uuid2", strategy = "uuid2")
        @Column(length = 36)
        private String uuid;
        

        这应该会对性能产生一些影响:

        • 消耗大小大于BINARY(16)
        • 在水合之后,java.lang.String 实例比 java.util.UUID 消耗更多的内存:UUID 作为字符串需要 112 个字节,而 UUID 需要 32 个字节(即两个 long + obj 标头)。

        但是使用字符串化的 UUID 更容易 - 更容易编写查询并且您可以看到表的内容。

        在 Hibernate 5.3 上测试

        【讨论】:

          【解决方案7】:

          使用当前 5.4.2 Hibernate 版本,

          如果您想要一个人类可读的 varchar(36) 字段在数据库表中,
          还有一个 Serializable UUID 数据类型在您的 Java 类中,
          你可以使用@Type(type = "uuid-char")同时你用java.util.UUID类型声明你的字段成员。

          请注意,@Column(length = 36) 对于将 MySQL 中的字段长度从 255 减少到 36 很重要。

          请注意,对于 PostgreSQL,您应该改用 @Type(type = "pg-uuid")

          import org.hibernate.annotations.Type
          import java.util.UUID
          import javax.persistence.Column
          import javax.persistence.GeneratedValue
          import javax.persistence.Id
          
          @Id @GeneratedValue
          @Type(type = "uuid-char") @Column(length = 36)
          private UUID id;
          

          【讨论】:

          • 这终于对我有用了,谢谢! :) (在 java 中使用 UUID,在 SQL 中使用 varchar(36) 和 spring boot 2.1.1)
          • 虽然我发现这会生成一个 uuid,即使给定的实体已经有一个来自代码的实体
          【解决方案8】:
          @Id
          @GeneratedValue(generator = "uuid")
          @GenericGenerator(name = "uuid", strategy = "uuid")
          @Column(name = "UUID_ID")
          public String getId(){
          return id;
          }
          

          这是在 Hibernate 5.0.11.FINAL 中为 uuid 生成器使用注释的正确方法。

          注意: IT 已被弃用。

          【讨论】:

            猜你喜欢
            • 2015-12-14
            • 1970-01-01
            • 1970-01-01
            • 2016-09-20
            • 1970-01-01
            • 2023-03-26
            • 2013-09-30
            • 1970-01-01
            相关资源
            最近更新 更多