English 中文(简体)
删除“一个Tomany”中藏匿物
原标题:On delete set null in hibernate in @OneToMany

我有一个部门实体,关系如下:

  1. Many departments can be in one parent department:

    @ManyToOne
    @JoinColumn(name = "ik_parent_department_id")
    private Department parentDepartment;
    
  2. www.un.org/Depts/DGACM/index_spanish.htm 1个上级部门可以有many Province:

    @OneToMany(mappedBy = "parentDepartment")
    private Set<Department> children = new HashSet<Department>(0);
    

接下来,我想执行: 当Idelete a department时,ik_jun_department_id para amount of allchildren of this department is set to null。 任何关于如何这样做的想法?

最佳回答

You ll have to set the children s ik_parent_department_id to null explicitly.

Department parentDepartment = (Department) session.load(Department.class, id);
session.delete(parentDepartment);
for (Department child : parentDepartment.getChildren()){
    child.setParentDepartment(null);
} 
session.flush();

随行,你只能设法删除<条码><部门/代码>。

问题回答

与日本宇宙开发公司合作,在母体Entity上,你可能拥有类似的东西。

@OneToMany(mappedBy="parent", cascade={CascadeType.PERSIST})
Collection<Child> children;

为了避免重复“一成不变的法典”和“安乐”;在“母体编码”中执行关于父母分离的违反诚信的例外规定。

@PreRemove
private void preRemove() {
   children.forEach( child -> child.setParent(null));
}

Just code it:

for (Department child : parent.getChildren()) {
    child.setParentDepartment(null);
}
session.delete(parent);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签