# @ManyToMany: cascade ALL vs {PERSIST, MERGE}
# Hibernate ORM 8.0.0.CR1, H2, JDK 25

== CourseBad (cascade = ALL): one student enrolled in two courses
-- remove(algebra); flush()
   sql> select cb1_0.id,cb1_0.title from CourseBad cb1_0 where cb1_0.id=?
   sql> select s1_0.course_id,s1_1.id,s1_1.name from course_bad_students s1_0 join Student s1_1 on s1_1.id=s1_0.student_id where s1_0.course_id=?
   sql> delete from course_bad_students where course_id=?
   sql> delete from Student where id=?
   threw: org.hibernate.exception.ConstraintViolationException <- org.h2.jdbc.JdbcBatchUpdateException
   cause: Referential integrity constraint violation: "FKHFU87UUUVAK0UGR0D4KGGPNTC: PUBLIC.COURSE_BAD_STUDENTS FOREIGN KEY(STUDENT_ID) REFERENCES PUBLIC.STUDENT(ID) (CAST(1 AS BIGINT))"; SQL statement:
raw rows after rollback: CourseBad=2 Student=1

== CourseBad: the same, but the student is only in that one course
-- remove(chemistry); flush()
   sql> select cb1_0.id,cb1_0.title from CourseBad cb1_0 where cb1_0.id=?
   sql> select s1_0.course_id,s1_1.id,s1_1.name from course_bad_students s1_0 join Student s1_1 on s1_1.id=s1_0.student_id where s1_0.course_id=?
   sql> delete from course_bad_students where course_id=?
   sql> delete from Student where id=?
   sql> delete from CourseBad where id=?
raw rows: Student 2 -> 1  (the student was deleted with the course)

== CourseGood (cascade = {PERSIST, MERGE})
-- remove(dance); flush()
   sql> select cg1_0.id,cg1_0.title from CourseGood cg1_0 where cg1_0.id=?
   sql> delete from course_good_students where course_id=?
   sql> delete from CourseGood where id=?
raw rows: Student 3 -> 3  course_good_students=0
