gpt4 book ai didi

java - JPA-多对一级联

转载 作者:行者123 更新时间:2023-12-01 16:02:23 26 4
gpt4 key购买 nike

我有两个实体

ServiceDownload.java

@Entity
public class ServiceDownload implements Serializable {
private static final long serialVersionUID = -5336424090042137820L;

@Id
@GeneratedValue
private Long id;
@Length(max = 255)
private String description;

private byte[] download;

private String fileName;

@ManyToOne
private Service service;

服务.java

@Entity
public class Service implements Serializable {
private static final long serialVersionUID = 4520872456865907866L;


@EmbeddedId
private ServiceId id;

@Length(max = 255)
private String servicename;

@Column(columnDefinition = "text")
private String highlightsText;
@Column(columnDefinition = "text")
private String detailsText;
@Column(columnDefinition = "text")
private String productText;
@Column(columnDefinition = "text")
private String dataText;

@ManyToMany(mappedBy = "services")
private Set<Machine> machines;

@OneToMany(targetEntity = ServiceDownload.class)
private List<ServiceDownload> serviceDownloads;

@OneToOne
private ServicePicture servicePicture;

当我创建一个新的 ServiceDownload 对象并尝试保留此对象时,我收到重复的键异常。看来jpa试图将一个新的服务对象插入到服务表中。如何禁用此行为?

最佳答案

您正在为您的@Id 使用@GenerateValue 注释。根据 JPA 文档,您应该提供唯一标识符

By default, the application is responsible for supplying and setting entity identifiers (see @Id)

尝试使用 @SequenceGenerator 和数据库中的序列来生成唯一标识符

关于java - JPA-多对一级联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3502297/

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