Hibernate: How to ensure that a parent is not deleted when all its child entities are?

前端 未结 2 2074
说谎
说谎 2021-01-26 08:22

I am deleting entities and hence rows from my database.

I do want to delete a certain entity and all of its child rows. However, I

2条回答
  •  生来不讨喜
    2021-01-26 08:41

    Switched your CascadeType.ALL operation from child table to parent table according to your need.

    Here is the simple solution which is almost similar yours, and customize it based on your need.

    @Entity(name = "Kennel")
    @Table(name = "Kennel")
    public class Kennel {
    
        @Id
        @Column(name = "Kennel_Id")
        private long kennelId;
    
        @Column(name = "Kennel_name")   
        private String KennelName;
    
        //cascade = {CascadeType.REMOVE} OR orphanRemoval = true    
       @OneToMany(cascade = {CascadeType.ALL},orphanRemoval = true,fetch =  fetchType.LAZY,mappedBy="dogKennel")    
        private Set dogSet;
        public long getKennelId() {
            return kennelId;
        }
    
        public void setKennelId(long kennelId) {
            this.kennelId = kennelId;
        }
    
        public String getKennelName() {
            return KennelName;
        }
    
        public void setKennelName(String kennelName) {
            KennelName = kennelName;
        }
    
        public Set getDogSet() {
            return dogSet;
        }
    
        public void setDogSet(Set dogSet) {
            this.dogSet = dogSet;
        }
    
    }
    
    @Entity(name = "Dog")
    @Table(name = "Dog")
    public class Dog {
    
        @Id
        @Column(name = "Dog_Id")
        private int dogId;
    
        @ManyToOne  
        @JoinColumn(name = "Dog_Kennel_Id",referencedColumnName = "Kennel_Id")
        private Kennel dogKennel;
    
        @Column(name = "Dog_Name")
        private String dogName;
    
        public int getDogId() {
            return dogId;
        }
    
        public void setDogId(int dogId) {
            this.dogId = dogId;
        }
    
        public Kennel getDogKennel() {
            return dogKennel;
        }
    
        public void setDogKennel(Kennel dogKennel) {
            this.dogKennel = dogKennel;
        }
    
        public String getDogName() {
            return dogName;
        }
    
        public void setDogName(String dogName) {
            this.dogName = dogName;
        }
    
    }
    
    
    
    //adding kennel=1 and dog=1
            Kennel ken=new Kennel();
            ken.setKennelId(1);
            ken.setKennelName("KennelExample1");        
            HashSet myDogSet=new HashSet();       
            Dog dog=new Dog();
            dog.setDogId(1);
            dog.setDogName("ExampleDog1");  
            dog.setDogKennel(ken);
            myDogSet.add(dog);
            ken.setDogSet(myDogSet);        
            kennelRepo.save(ken);
    
            //adding dog=2 under kennel=1
            Dog dog2=new Dog();
            dog2.setDogId(2);
            dog2.setDogName("ExampleDog2"); 
            Kennel dog2ken=new Kennel();
            dog2ken.setKennelId(1);     
            dog2.setDogKennel(dog2ken);
            dogRepo.save(dog2);
    
            //adding dog=3 under kennel=1
            Dog dog3=new Dog();
            dog3.setDogId(3);
            dog3.setDogName("ExampleDog3");
            Kennel dog3ken=new Kennel();
            dog3ken.setKennelId(1);
            dog3.setDogKennel(dog3ken);
            dogRepo.save(dog3);
    
            //deleting dog=3 
            dog3=new Dog();
            dog3.setDogId(3);
            dogRepo.delete(dog3);
    
            //deleting kennel=1 which in turn delete all dogs under
            Kennel k=kennelRepo.findByKennelId(1);
            kennelRepo.delete(k);
    

提交回复
热议问题