joemccall86 opened a new issue, #14585:
URL: https://github.com/apache/grails-core/issues/14585

   ### Task List
   
   - [x] Steps to reproduce provided
   - [x] Stacktrace (if present) provided
   - [x] Example that reproduces the problem uploaded to Github
   - [x] Full description of the issue provided (see below)
   
   ### Steps to Reproduce
   
   1. Create domain classes with a many-to-many relationship
   2. Attempt to clean up the generated join table inside of the 
`beforeDelete()` method (utilizing `withNewSession` as outlined here: 
http://gorm.grails.org/6.0.x/hibernate/manual/#_the_beforedelete_event)
   3. Attempt to delete one side of the relationship
   
   ### Expected Behaviour
   
   The `beforeDelete()` method is able to remove the object being deleted from 
other associations in the database.
   
   ### Actual Behaviour
   
   An exception is thrown when the session is flushed:
   
   ```
   Hibernate operation: could not execute statement; SQL [n/a]; Referential 
integrity constraint violation: "FKGHKKY8WMH379RPMFH92T807RY: 
PUBLIC.TEAM_MEMBERS FOREIGN KEY(PERSON_ID) REFERENCES PUBLIC.PERSON(ID) (1)"; 
SQL statement:
   delete from person where id=? and version=? [23503-194]; nested exception is 
org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: 
"FKGHKKY8WMH379RPMFH92T807RY: PUBLIC.TEAM_MEMBERS FOREIGN KEY(PERSON_ID) 
REFERENCES PUBLIC.PERSON(ID) (1)"; SQL statement:
   delete from person where id=? and version=? [23503-194]
   ```
   
   ### Notes
   
   Following the advice from [GORM Gotchas (Part 
2)](https://spring.io/blog/2010/07/02/gorm-gotchas-part-2/) I understand that 
many-to-many relationships do not cascade deletions, so I need to manage them 
on my own. The domain's `beforeDelete()` method seemed like the perfect place 
to document this and other side-effects of deleting an instance. 
   
   If this is not possible I think there should be a section in the 
documentation that explains a recommended way of cleaning up associated join 
tables for many-to-many relationships. I think this situation is common enough 
to warrant it. I don't mind writing that section if it's confirmed that the 
sample project exhibits correct behavior.
   
   ### Environment Information
   
   - **Operating System**: Ubuntu MATE 17.10
   - **GORM Version:** 6.1.8.RELEASE, 6.0.13.RELEASE
   - **Grails Version (if using Grails):** 3.2.12
   - **JDK Version:** `openjdk version "1.8.0_162"`
   
   ### Example Application
   
   - 
https://github.com/joemccall86/cascade-delete-test/tree/automatic-collection-purge
   - Specifically the `automatic-collection-purge` branch
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to