gpt4 book ai didi

java - 允许 ID = 0 与 Hibernate ("eclipselink.id-validation"= "NULL"等效)

转载 作者:行者123 更新时间:2023-11-30 10:13:40 26 4
gpt4 key购买 nike

我正在使用 hibernate 5.0.12 和 springboot 1.5.14。

我的实体有这个id

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "UserID", nullable = true)
public int getUserId() {
return this.userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

在我的数据库中有 id = 0 的礼物记录。

如果我读取、编辑和保存这些记录,hibernate 会使用生成的新 ID 复制这些记录。

我如何配置 hibernate 以允许 id = 0 值?

在eclispelink中这个配置是"eclipselink.id-validation"= "NULL"

最佳答案

解决方案是将主键类型声明为 Integer 而不是 int。

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "UserID", nullable = true)
public Integer getUserId() {
return this.userId;
}

public void setUserId(Integer userId) {
this.userId = userId;
}

关于java - 允许 ID = 0 与 Hibernate ("eclipselink.id-validation"= "NULL"等效),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51242313/

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