JPA: DELETE WHERE does not delete children and throws an exception

后端 未结 5 1073
渐次进展
渐次进展 2020-12-08 14:42

I am trying to delete a large number of rows from MOTHER thanks to a JPQL query.

The Mother class is defined as follows:

@E         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-08 14:58

    You could relay on the RDBMS to delete those Mothers using foreign key constraint. This assumes your generateing your DDL from entities:

    @Entity
    @Table(name = "CHILD")
    public class Child  implements Serializable {
    
        @ManyToOne
        @JoinColumn(name = "MOTHER_ID", foreignKey = @ForeignKey(foreignKeyDefinition =
            "FOREIGN KEY(MOTHER_ID) REFERENCES MOTHER(ID) ON DELETE CASCADE",
            value = ConstraintMode.CONSTRAINT))
        private Mother mother;    
    }
    

提交回复
热议问题