gpt4 book ai didi

java - 带有连接表的双向 @ManyToOne 创建重复键

转载 作者:行者123 更新时间:2023-11-30 03:05:12 25 4
gpt4 key购买 nike

我正在使用 hibernate 实现与连接表@ManyToOne 双向关系,但是当我在保存一些数据时,hibernate 声称关系表中的记录被插入了两次,违反了唯一约束,如下面的错误消息所示:

ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ERROR: duplicate key value violates unique constraint "tillage_sample_pkey"
Detail: Key (id_tillage, id_sample)=(82, 110) already exists.

我有下表:

  • 耕作(id,一些其他数据)(一次耕作可以有多个样本)
  • 样本(id,一些其他数据)(一个样本只能有一次耕作)
  • tillage_sample(id_tillage、id_sample)PK(id_tillage、id_sample)

当我创建耕作对象时,我会填充样本。在示例对象中,我指向耕作对象,创建“双重绑定(bind)”

我猜这个“双重绑定(bind)”造成了麻烦,因为在保存耕作时,耕作/样本关系由 hibernate 保存,并在尝试将耕作保留在内部时重复该步骤。样本(这是相同的耕作对象)。

这是我的代码,以帮助您理解我的问题:

Tillage.java

@Entity
@Table(name = "tillage")
public class Tillage implements Serializable {

private static final long serialVersionUID = 3605331584324240290L;

@Id
@GeneratedValue(generator = "tillage_id_seq", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "tillage_id_seq", sequenceName = "tillage_id_seq", allocationSize = 1)
private Integer id;

@Column(name = "name")
private String name;
// Other simple attributes

@ManyToOne
@JoinColumn(name = "id_farm")
@JsonBackReference
private Farm farm;

// This relation is the problematic one
@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "tillage_sample",
joinColumns = { @JoinColumn(name = "id_tillage") },
inverseJoinColumns = {@JoinColumn(name = "id_sample") })
private List<Sample> sampleList;

// Although similar, this one is doing OK
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "tillage_owner",
joinColumns = { @JoinColumn(name = "id_tillage") },
inverseJoinColumns = {@JoinColumn(name = "id_owner") })
private List<Owner> ownerList;

// getters & setters
}

示例.java

@Entity
@Table(name = "sample")
public class Sample implements Serializable {

private static final long serialVersionUID = 7064809078222302493L;

@Id
@GeneratedValue(generator = "sample_id_seq", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "sample_id_seq", sequenceName = "sample_id_seq", allocationSize = 1)
private Integer id;

@Column(name = "name")
private String name;
// Other simple attributes

// This completes the relation Tillage-Sample
@ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "tillage_sample",
joinColumns = { @JoinColumn(name = "id_sample") },
inverseJoinColumns = {@JoinColumn(name = "id_tillage") })
private Tillage tillage = new Tillage();

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable(name = "sample_sample_item",
joinColumns = { @JoinColumn(name = "id_sample") },
inverseJoinColumns = {@JoinColumn(name = "id_sample_item") })
private List<SampleItem> sampleItemList;


// Getters and Setters
}

SomeService.java

...
@Override
public Tillage toTillage(TillageDTO dto) {

Tillage tillage = new Tillage();
tillage.setName(dto.getNameTillage());

// Fill the samples of the tillage
for(ArrSample sample : dto.getSamples().getArrSample()){

Sample s = new Sample();
s.setName(sample.getName());

// Setting the tillage in the Sample object
s.setTillage(tillage);

// Fill the items of the sample
for(Array arr : sample.getAreas().getArray()){

SampleItem si = new SampleItem();

si.setProduction(Double.parseDouble(arr.getProduction()));

// Double binding between sample and sampleItem
si.setSample(s);
s.getSampleItemList().add(si);
}
// Adding a sample to Tillage
tillage.getSampleList().add(s);
}
return tillage;
}


public void save(TillageDTO dto){
Tillage t = this.toTillage(dto);

// The error occurs when we persist the data
// The entityManager is Autowired by Spring and works in other places
entityManager.persist(tillage);

}

最佳答案

这不是双向 OneToMany。使用相同的连接表的单向关联过于独立。

在双向关联中,一侧必须与另一侧相反。对于 OneToMany,一侧必须是相反一侧:

@OneToMany(mappedBy = "tillage", cascade=CascadeType.ALL, fetch = FetchType.EAGER)
private List<Sample> sampleList;

关于java - 带有连接表的双向 @ManyToOne 创建重复键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34984628/

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