gpt4 book ai didi

java - 如何在 Java Hibernate 中将 id 设为字符串?

转载 作者:行者123 更新时间:2023-12-02 01:28:43 27 4
gpt4 key购买 nike

我有以下代码:

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;

import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
import java.io.Serializable;

@Data
@RequiredArgsConstructor
@AllArgsConstructor
@MappedSuperclass
public abstract class BaseEntity implements Serializable {

@Id
@GeneratedValue
private final String id = null; // there's Long data type originally

@Version
private Long version;

}

如果我运行它,我会得到:

Caused by: org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.String

如果我删除 @GeneratedValue我仍然得到注释:

Caused by: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save()

问题是我的对象可能包含 id 下的字符串值字段所以我想改变 LongString .

最佳答案

我应该使用以下两行:

@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")

而不是

@GeneratedValue

关于java - 如何在 Java Hibernate 中将 id 设为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57565018/

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