gpt4 book ai didi

java - Hibernate OneToMany mappedby 保存时插入重复记录

转载 作者:行者123 更新时间:2023-11-29 08:59:49 28 4
gpt4 key购买 nike

我在 Parent 中提供了一个简单的 oneToMany 关系,并在 Chile 实体类中提供了相应的 ManyToOne:

家长:

@Entity
@Table(name = "FormExtraInfo")
@PrimaryKeyJoinColumn(name="form_container_id")
public class Form extends Container {

private List<Reason> reasons = new ArrayList<Reason>();

@OneToMany(mappedBy="form",cascade={javax.persistence.CascadeType.ALL},orphanRemoval=true)
@Cascade(value={CascadeType.ALL})
public List<Reason> getReasons() {
return reasons;
}

public void setReasons(List<Reason> reasons) {
this.reasons = reasons;
}

public void addReason(Reason reason) {
if (this.reasons == null) {
this.reasons = new ArrayList<Reason>();
}

this.reasons.add(reason);
}

子类:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="Container_id")
public Form getForm() {
return form;
}

public void setForm(Form form) {
this.form = form;
}

Action 类:

//Set the reasons
String[] reasonStatus = strutsForm.getMultiValueProperty(REASON_STATUS);
String[] reasonText = strutsForm.getMultiValueProperty(REASON_TEXT);

List<Reason> reasons = new ArrayList<Reason>();

logger.debug("form container ID : " + form.getId() +". # of Reasons for this form: "+ reasonText.length);

for (int i = 0; i < reasonText.length; i++) {
Reason r = new Reason();
r.setComment(reasonText[i]);
r.setStatusTypeCode(reasonStatus[i]);
r.setForm(form);
reasons.add(r);
}
form.setReasons(reasons);

示例案例:Status_code 原因_文本abc abc1xyz xyz1

保存表格:Status_code 原因_文本abc abc1xyz xyz1abc abc1xyz xyz1

对于任何操作:新的插入或删除或更新,它首先将旧数据复制到数据库,然后是我执行的操作。

最佳答案

尝试为此替换级联子句

@Cascade (value={CascadeType.SAVE_UPDATE,CascadeType.DELETE_ORPHAN})

看看我关于映射一对多的博文 http://arecordon.blogspot.com.ar/2013/05/hibernate-mapping-associations-one-to_20.html

关于java - Hibernate OneToMany mappedby 保存时插入重复记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18137265/

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