Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 23rd, 2012  |  syntax: None  |  size: 0.89 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Removing child-parent association in GORM
  2. class Business{
  3.     static hasMany = [contacts:ContactPerson]
  4. }
  5.  
  6. class ContactPerson{
  7. }
  8.        
  9. bob.delete(flush:true)
  10.        
  11. ERROR: update or delete on table "contact_person" violates foreign key constraint
  12.     "fk4a69c6b329ef2fe1" on table "business_contact_person"
  13. Detail: Key (id)=(174) is still referenced from table "business_contact_person".
  14.        
  15. class Business{
  16.   static hasMany = [ contacts:ContactPerson ]
  17. }
  18.  
  19. class ContactPerson{
  20.   static belongsTo = [ business: Business ]
  21. }
  22.        
  23. def bob = ContactPerson.get(params.id)
  24.  
  25. def criteria = Business.createCriteria()
  26.  
  27. def businesses = criteria.listDistinct{
  28.     createAlias("contactPersons","c")
  29.     eq("c.id", bob.id)
  30. }
  31.  
  32. businesses.each{business->
  33.     business.removeFromContactPersons(bob)
  34.     business.save(flush:true)
  35. }
  36.  
  37. bob.delete(flush:true)
  38.        
  39. static mapping = {
  40.     children cascade:"all-delete-orphan"
  41. }