gpt4 book ai didi

jpa - 字符串 id 生成器

转载 作者:行者123 更新时间:2023-12-04 00:35:59 24 4
gpt4 key购买 nike

在 jpa 中实现字符串 id 的最简单方法是什么?
到目前为止,我所拥有的是

@Id
@GeneratedValue
private int id;

我想要的是类似的东西
@Id
@GeneratedValue
private String id;

但如果我像这样使用它,我会得到“这个 id 生成器生成长、整数、短”。

最佳答案

您可以像这样从 Java 创建 UUID:

UUID.randomUUID().toString();

或者,如果您的 JPA 支持它,就像 Hibernate 那样,您可以使用:
@Id @GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
private String myId;

Checkout this blogpost for details.

如果您在 google 上搜索“JPA UUID”,则有很多选择。

关于jpa - 字符串 id 生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13701105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com