Spring 春季冬眠插入父级和子级
我的程序要求是向数据库中添加一个新的父项以及子项。到目前为止,我在父类中有此方法:Spring 春季冬眠插入父级和子级,spring,hibernate,Spring,Hibernate,我的程序要求是向数据库中添加一个新的父项以及子项。到目前为止,我在父类中有此方法: @Transient public void addLecturer(Lecturer lecturer) { if(lecturers == null) { lecturers = new ArrayList<Lecturer>(); } lecturer.setChecker(this); this.lecturers.add(lect
@Transient
public void addLecturer(Lecturer lecturer) {
if(lecturers == null) {
lecturers = new ArrayList<Lecturer>();
}
lecturer.setChecker(this);
this.lecturers.add(lecturer);
}
我的控制器:
@RequestMapping(value="/matching_page", method = RequestMethod.GET)
public ModelAndView get(@ModelAttribute("checker") Lecturer checker, BindingResult
result) {
ArrayList<String> lecturers = new ArrayList<String>();
lecturers.add("Somma");
lecturers.add("Trina");
lecturers.add("Adam");
lecturers.add("Eve");
HashMap<String, ArrayList<String>> model = new HashMap<String
ArrayList<String>>();
model.put("lecturerList", lecturers);
return new ModelAndView("matching_page", "model", model);
}
@RequestMapping(value="/matching_page", method = RequestMethod.POST)
public ModelAndView hello(@ModelAttribute("checker") Lecturer checker,
BindingResult result) {
lecturerService.addChecker(checker);
return new ModelAndView ("redirect:/admin");
}
我的模型
@Entity
@Table(name = "lecturers")
@Component
public class Lecturer implements Serializable{
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="checker_id")
private Lecturer checker;
@OneToMany(mappedBy="checker", orphanRemoval=true)
private List<Lecturer> lecturers = new ArrayList<Lecturer>();
}
@实体
@表(name=“讲师”)
@组成部分
公共类讲师实现可序列化{
@多通(级联=级联类型.ALL)
@JoinColumn(name=“checker\u id”)
私人讲师检查员;
@OneToMany(mappedBy=“checker”,orphan=true)
私有列表讲师=新ArrayList();
}
表单绑定了一个父对象(检查器),我想添加一个或多个从“选择”列表中选择并保存的子对象
“保存”可以工作,但它会将所有新对象保存为子对象,并且不会使用检查器id单独保存检查器父对象。我不确定问题出在哪里,我想知道。发布模型定义。我猜你的问题是因为你奇怪地试图把“父母”和“孩子”联系起来 有了hibernate,您只需要做一方面的工作,而hibernate将负责另一方面的工作。例如:
public class MyObject {
@Id
private UUID id;
@ManyToOne
@JoinColumn(name = "myColumnName")
private MyObject parent;
@OneToMany(mappedBy = "parent")
private Set<MyObject> children;
public void addChild(MyObject child) {
children.add(child);
}
public void removeChild(MyObject child) {
children.remove(child);
}
// Other Accessors
}
实际上,我不必设置每个孩子的家长,因为Hibernate将为我处理这个问题。我所需要做的就是把每个孩子放入父母的孩子集合中。如果对一个既非暂时又附加的实体进行插入,仅此操作就足以导致插入发生
@Transactional
public void update(parentId,child) {
myDaoClass.readById(parentId).addChild(child);
// When the current transaction ends, hibernate will issue a commit to persist the
// child that we added (created) here. No need to call session.persist(...) on it.
// Assuming no exceptions are thrown, of course.
}
您说它会将所有新对象保存为子对象……但当父对象(checker)未保存时,它如何做到这一点(这意味着它们有一个父对象,因此为checker\u id
),谢谢您的回复。我现在仔细考虑你的建议。同时,我编辑了我的问题,添加了模型。这是一节课,模型看起来不错。我相信你有一个id列和其他数据(电子邮件、姓名等)的列注释,只是为了简洁起见删掉了它们?
@Entity
@Table(name = "lecturers")
@Component
public class Lecturer implements Serializable{
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="checker_id")
private Lecturer checker;
@OneToMany(mappedBy="checker", orphanRemoval=true)
private List<Lecturer> lecturers = new ArrayList<Lecturer>();
}
public class MyObject {
@Id
private UUID id;
@ManyToOne
@JoinColumn(name = "myColumnName")
private MyObject parent;
@OneToMany(mappedBy = "parent")
private Set<MyObject> children;
public void addChild(MyObject child) {
children.add(child);
}
public void removeChild(MyObject child) {
children.remove(child);
}
// Other Accessors
}
public MyObject create(MyObject parent) {
// Obtain a collection of MyObject that you would like to associate as "parent's" children
for (MyObject curObject : myListOfObjects)
parent.addChild(curObject);
sessionFactory.getCurrentSession().persist(parent);
}
@Transactional
public void update(parentId,child) {
myDaoClass.readById(parentId).addChild(child);
// When the current transaction ends, hibernate will issue a commit to persist the
// child that we added (created) here. No need to call session.persist(...) on it.
// Assuming no exceptions are thrown, of course.
}