我在Parent和智利实体类中的对应ManyToOne中提供了一个简单的oneToMany关系:
上级:
@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;
}
动作类:
//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);
示例案例:
状态码原因文本
abc abc1
xyz xyz1
保存表格:
状态码原因文本
abc abc1
xyz xyz1
abc abc1
xyz xyz1
使用任何操作:新插入,删除或更新,它首先将旧数据复制到DB,然后再复制我执行的操作。
最佳答案
尝试为此替换级联子句
@Cascade (value={CascadeType.SAVE_UPDATE,CascadeType.DELETE_ORPHAN})
看看我的有关一对多映射http://arecordon.blogspot.com.ar/2013/05/hibernate-mapping-associations-one-to_20.html的博客文章