【问题标题】:How can I make a id to be String in Java Hibernate? [duplicate]如何在 Java Hibernate 中将 id 设为 String? [复制]
【发布时间】:2019-12-25 03:53:20
【问题描述】:

我有以下代码:

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 字段下包含字符串值,所以我想将Long 更改为String

【问题讨论】:

    标签: java hibernate


    【解决方案1】:

    我应该使用以下两行:

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

    而不是

    @GeneratedValue
    

    【讨论】:

      猜你喜欢
      • 2016-03-11
      • 2019-05-10
      • 2012-05-01
      • 2019-12-03
      • 2012-09-15
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多