【发布时间】:2017-06-23 22:11:44
【问题描述】:
在双向 OneToMany 关系中,如何在不调用父级所有子级的情况下删除子级?
由于我的应用程序有点大,我创建了一个示例项目来重现问题。我有 2 个实体,一个父级和一个子级。
家长:
package com.example.entity;
import javax.persistence.*;
import java.util.List;
@Entity
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(
mappedBy = "parent",
cascade = CascadeType.ALL,
fetch = FetchType.LAZY,
orphanRemoval = true
)
private List<Child> children;
}
Getter 和 Setter 未显示!
孩子:
package com.example.entity;
import javax.persistence.*;
@Entity
public class Child {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "parent_id")
private Parent parent;
}
Getter 和 Setter 未显示!
我也有 2 个存储库:
ChildRepository:
package com.example.dao;
import com.example.entity.Child;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ChildRepository extends JpaRepository<Child, Long> {}
ParentRepsitory: 看起来一模一样。
现在我创建了一个测试来重现问题:
存储库测试:
package com.example;
import com.example.dao.ChildRepository;
import com.example.dao.ParentRepository;
import com.example.entity.Child;
import com.example.entity.Parent;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.transaction.Transactional;
import java.util.Arrays;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RepositoryTest {
@Autowired
private ChildRepository childRepository;
@Autowired
private ParentRepository parentRepository;
@Test
@Transactional
public void test() {
Parent parent = new Parent();
Child child1 = new Child();
Child child2 = new Child();
parent.setChildren(Arrays.asList(child1, child2));
Assert.assertEquals(0, parentRepository.count());
Assert.assertEquals(0, childRepository.count());
parentRepository.save(parent);
Assert.assertEquals(1, parentRepository.count());
Assert.assertEquals(2, childRepository.count());
childRepository.delete(child1);
Assert.assertEquals(1, parentRepository.count());
Assert.assertEquals(1, childRepository.count());
}
}
但是Child 永远不会被删除,计数仍然是 2!我读过很多关于或多或少相同问题的问题,答案总是一样的:
parent.getChildren().remove(child1);
parentRepository.save(parent);
这不可能吗?如果我的父母有数千个孩子,我必须从数据库中获取所有孩子才能删除一个?最尴尬的是,如果实体没有被删除,为什么不会抛出任何错误?那么正确的方法是什么?完全从父级中删除列表并始终手动调用子级以便没有外键?我从早餐开始就试图解决这个问题,我正在绕圈子。
编辑:添加休眠日志:
Hibernate: select count(*) as col_0_0_ from parent parent0_
Hibernate: select count(*) as col_0_0_ from child child0_
Hibernate: insert into parent (id) values (default)
Hibernate: insert into child (id, parent_id) values (default, ?)
Hibernate: insert into child (id, parent_id) values (default, ?)
Hibernate: select count(*) as col_0_0_ from parent parent0_
Hibernate: select count(*) as col_0_0_ from child child0_
Hibernate: select count(*) as col_0_0_ from parent parent0_
Hibernate: select count(*) as col_0_0_ from child child0_
【问题讨论】:
-
问题是你在一个事务中做这件事。如果您结束交易并检查数据库,删除应该是有效的。
-
@ByeBye nope 很快就对其进行了测试,正如您从添加的休眠日志中看到的那样,从未触发过删除。
-
因为您的 child1 实体与这个新创建的实体没有任何联系。在
childRepository.delete(child1)的时刻只是空实体。