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
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.
grails groovy foreign-keys cascading-deletes
Dave
source share