gpt4 book ai didi

java - Spring 数据 : How to set GenerationType strategy while working with @Embeddable and @Embedded

转载 作者:行者123 更新时间:2023-11-30 02:39:38 25 4
gpt4 key购买 nike

我正在尝试在 Spring 数据中使用 @Embeddable@Embedded 。以下是我的方法。

设备实体:

@Entity
@Table(name = "device")
public class DeviceEntity implements Serializable {


private static final long serialVersionUID = 1L;

@EmbeddedId
@AttributeOverrides({@AttributeOverride(name = "id" , column= @Column(name = "id",nullable = false))})
private DeviceIdType deviceIdType;

.

DeviceIdType:

@Embeddable
public class DeviceIdType implements Serializable {

private static final long serialVersionUID = 1L;

@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

。。.

错误消息:

"code": "4715",
"message": "Something went wrong",
"additionalInfo": "null id generated for:class com.b.DeviceEntity; nested exception is org.hibernate.id.IdentifierGenerationException: null id generated for:class com.b.DeviceEntity"

现在,当我使用 GenertionType 策略时,我收到此异常。

因此,我的问题是:在使用 @Embeddable 和 @Embedded 时如何设置 GenerationType 策略?

最佳答案

@Embeddable@EmbeddedId 大多数时候,在 JPA/Spring Data 中用于复合主键。

如果您有一个简单的生成主键,最好将 @Id 注释与 @GenerationType 一起使用

关于java - Spring 数据 : How to set GenerationType strategy while working with @Embeddable and @Embedded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42116532/

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