java 如何在spring Boot 2中生成UUID

ua4mk5z4  于 2023-03-11  发布在  Java
关注(0)|答案(1)|浏览(246)

你好StackOverflow社区
我正在使用spring Boot 2.7和mysql 8。我正在使用hib生成一个列,其中包含UUID值(与我的主键不同),但收到此错误

not-null property references a null or transient value

代码

@GeneratedValue(generator = "UUID")
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    @Column(name = "campaignId", updatable = false, nullable = false)
    @Type(type = "org.hibernate.type.UUIDCharType")
    private UUID campaignId;

尝试了各种事情,但似乎没有工作。我只是需要系统生成一个唯一的ID。

wljmcqd8

wljmcqd81#

它应该简单地“工作”与此:

@Entity
class Campaign {
    @Id
    @GeneratedValue
    private UUID campaignId;

    . . .
}

如果必须更明确,则还要定义列规范:

@Entity
class Campaign {
    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(name = "campaignId", columnDefinition = "VARCHAR(36)", nullable = false, updatable = false)
    @Type(type = "uuid-char")
    private UUID campaignId;

    . . .
}

相关问题