Using Hibernate UUIDGenerator via annotations

后端 未结 8 624
无人共我
无人共我 2020-12-04 10:31

I\'m using my uuid as following:

@Id
@GeneratedValue(generator = \"uuid\")
@GenericGenerator(name = \"uuid\", strategy = \"uuid\")
@Column(name = \"uuid\", u         


        
相关标签:
8条回答
  • 2020-12-04 11:20
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid")
    @Column(name = "UUID_ID")
    public String getId(){
    return id;
    }
    

    This is the proper way to use annotation for uuid generators in Hibernate 5.0.11.FINAL.

    Note: IT is deprecated.

    0 讨论(0)
  • 2020-12-04 11:29

    With current 5.4.2 Hibernate version,

    if you want a Human-Readable varchar(36) field in the database table,
    but also a Serializable UUID data type in your Java Class,
    you can use @Type(type = "uuid-char") at the same time you declare your field member with java.util.UUID type.

    Note that @Column(length = 36) is important to reduce from 255 to 36 the field length in MySQL.

    Note that with PostgreSQL you should use @Type(type = "pg-uuid") instead.

    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;
    
    0 讨论(0)
提交回复
热议问题