Reputation: 6609
How to achieve on delete cascade
in database level with GORM mapping with foreign keys?
I don't mean cascade delete in application level (sequentialy delete done by GORM).
Upvotes: 2
Views: 6328
Reputation: 61
try adding static mapping with cascade: 'all-delete-orphan' in your domain class
e.g.
class Parent {
hasOne = [child: Child]
...
static mapping = {
child cascade: 'all-delete-orphan'
}
}
class Child {
belongsTo = [parent: Parent]
}
if you got time see this: http://grails.org/doc/2.3.7/guide/GORM.html#cascades
hope this helps
Upvotes: 5
Reputation: 24776
GORM is an object relational mapper and it's implementation and responsibilities are within the application layer of your application. Your request, to have cascade deletes handled by your RDBMS (Postgres or MySQL) falls outside of the responsibilities of GORM.
However, it's entirely possible to implement cascading behavior at the RDBMS using triggers. See the respective documentation for how that is done. In addition, you will need to disable hibernate's second level cache (since your database will become out of sync with hibernate due to these changes).
Finally, to assist you with maintaining your triggers you can use the database migrations plugin/feature of Grails.
Good luck, but be careful since you are circumventing GORM/Hibernate/Grails.
Upvotes: 3