Grails - multiple belongs to the same class with cascading deletion

This is for Grails users. I asked him on the grails - user mailing list, but I thought that since I had been struggling with this for several days, I should use the widest possible network.

I am having difficulty trying to model the relationship between two objects of the same type in another object (of another type), referencing two objects.

As an example of what I'm trying to do, suppose you model relationships between family members. Any given relationship is “owned” by two different family members. So:

class Person { hasMany[relationships: Relationship] static mappedBy = [relationships:'p1', relationships:'p2'] } class Relationship { Person p1 Person p2 String natureOfRelationship // for example, "cousins" static belongsTo = [p1: Person, p2: Person] } 

The goal is that if p1 or p2 is deleted, then the deletion will be a cascade for all objects of objects on the hasMany map. Instead, every time I try, I end up with a foreign key violation. I tried using the "cascading" attribute as described in the documentation:

http://grails.org/doc/1.0.x/guide/single.html#5.5.2.9%20Custom%20Cascade%20Behaviour

So, I decided to add this to the Person class:

 static mapping = { relationships cascade:'delete' } 

I also had no luck.

I also looked at the devDB.script file that Grails generates to see how it configured the foreign keys to Connect. If I manually add “ON. DELETE CASCADE” to restrict foreign keys, then it works fine, but explicitly making manual changes to the automatically generated database script is not the most reliable solution. Ideally, I would like to indicate this behavior using GORM.

So what should I do? Is there a way to force cascading multiple foreign keys / owners? Should I do this manually using the onDelete action for a person? Do I need to go into Hibernate configuration for this, or can I do it in Grails / GORM somehow?

Thank you very much for your time and for any help you can offer.

+7
grails groovy foreign-keys cascading-deletes
source share
2 answers

You can add beforeDelete to the Person class and request another parent. If the other parent does not exist, you can delete it. Please note that you are violating foreign key violations because you probably need to remove both parents, since the relationship has an FK for both of them.

+1
source share

You can also define 2 Relationshipcollections in Person

incomingRelations and outgoingRelations seem to be useful words to distinguish (if applicable to your domain).

You can determine the relationship of the properties of the transient process only with a getter that returns the union of both relationship relationships (unchanged so as not to change it / these changes most likely did not make sense)

 class Person { Relationship incomingRelations Relationship outgoingRelations static mappedBy = [incomingRelations:'p1', outgoingRelations:'p2'] static transients = ['relations'] Set getRelations() { //creates a new collection as union of the old ones return Collections.unmodifiableSet(this.incomingRelations + this.outgoingRelations) } } class Relationship { static belongsTo = [p1:Person, p2:Person] } 

if not suitable, I would try to make an even approach proposed by Miguel Pin

0
source share

All Articles