【问题标题】:spring data JPA deletion春季数据JPA删除
【发布时间】:2016-03-17 19:09:28
【问题描述】:

我在尝试删除记录时遇到外键违规问题。

我有这个记录:

@Entity
public class Parent {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long parentId;

    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "childId")
    private Child child;

@Entity
@Table(name = "Child")
public class Child {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long childId;


    public Long getOperatoryId() {
        return id;
    }

当我尝试删除孩子时,我遇到了密钥违规,因为有一些父记录指向孩子。我想我可以先删除父母,然后去删除孩子:

parentRepository.delete(父)

但我收到一个错误,即属性 ID 不存在于子级。这是因为子id被命名为childId而不是id吗?

【问题讨论】:

    标签: spring-data spring-data-jpa


    【解决方案1】:

    在这里,我处理了您要求的类似示例。根据您的需要对其进行自定义。

    • SQL Server 表创建查询

       CREATE TABLE "DBO"."Parent"(
       Parent_Id INT PRIMARY KEY NOT NULL,
       Parent_Name VARCHAR(255) NOT NULL)
      
       CREATE TABLE "DBO"."Child"(
       Child_Id INT PRIMARY KEY NOT NULL,
       Child_Name VARCHAR(255) NOT NULL,
       Parent_Ref_Id int not null,
       CONSTRAINT FK_Parent_Ref_Id FOREIGN KEY (Parent_Ref_Id) REFERENCES Parent(Parent_Id)
      )
      
    • Spring 数据 JPA 代码

    父实体

        @Entity(name = "Parent")
        @Table(name = "Parent")
        public class Parent {
    
        @Id
        @Column(name = "Parent_Id")
        private long parentId;
    
        @Column(name = "Parent_Name")   
        private String parentName;
    
        //cascade = {CascadeType.REMOVE} OR orphanRemoval = true    
        @OneToOne(cascade = {CascadeType.ALL},orphanRemoval = true,fetch = FetchType.LAZY,mappedBy="parentInfo")    
        private Child childInfo;
    
        public long getParentId() {
            return parentId;
        }
    
        public void setParentId(long parentId) {
            this.parentId = parentId;
        }
    
        public String getParentName() {
            return parentName;
        }
    
        public void setParentName(String parentName) {
            this.parentName = parentName;
        }
    
        public Child getChildInfo() {
            return childInfo;
        }
    
        public void setChildInfo(Child childInfo) {
            this.childInfo = childInfo;
        }
        }
    

    子实体

    @Entity(name = "Child")
    @Table(name = "Child")
    public class Child {
    
        @Id
        @Column(name = "Child_Id")
        private int childId;
    
        @Column(name = "Child_Name")
        private String childName;
    
        @OneToOne
        @JoinColumn(name = "Parent_Ref_Id", referencedColumnName = "Parent_Id")
        private Parent parentInfo;
    
        public int getChildId() {
            return childId;
        }
    
        public void setChildId(int childId) {
            this.childId = childId;
        }
    
        public String getChildName() {
            return childName;
        }
    
        public void setChildName(String childName) {
            this.childName = childName;
        }
    
        public Parent getParentInfo() {
            return parentInfo;
        }
    
        public void setParentInfo(Parent parentInfo) {
            this.parentInfo = parentInfo;
        }
    
    }
    

    儿童回购代码

    public interface ChildRepo extends CrudRepository<Child,Long> {
    
    }
    

    父回购代码

    public interface ParentRepo extends CrudRepository<Parent,Long> {
        Parent findByParentId(long id);
    }
    

    控制器代码

    @Autowired
    private final ParentRepo parentRepo;
    ....
    
    private void save() {
            //Save the parent
            Parent p = new Parent();
            p.setParentId(1);
            p.setParentName("Parent1");
            Child c = new Child();
            c.setChildId(1);
            c.setChildName("Child1");
            c.setParentInfo(p);
            p.setChildInfo(c);
            parentRepo.save(p);
    
            //delete the parent and child as well
            Parent p1 = parentRepo.findByParentId(1);
            parentRepo.delete(p1);
        }
    

    【讨论】:

      猜你喜欢
      • 2016-01-30
      • 2013-11-12
      • 2020-02-20
      • 1970-01-01
      • 1970-01-01
      • 2020-08-19
      • 2021-07-04
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多