title: Grails deleting child
body: <p>I have a class, DigitalObject which is basically a container for a url and the date that it was  last modified.</p>
  
  <pre><code>class DigitalObject {
    String url
    Date lastUpdated
  }
  </code></pre>
  
  <p>I have two classes that use DigitalObjects to reference different urls related to them</p>
  
  <pre><code>class Video {
     DigitalObject englishVersion
     DigitalObject frenchVersion
  }
  
  class Image {
     DigitalObject thumbnailImage
     DigitalObject fullSizeImage
  }
  </code></pre>
  
  <p>I'm having a ton of trouble getting the mapping correct.</p>
  
  <p>I want to be able to delete a DigitalObject and have GORM take care of removing it from it's  parent.</p>
  
  <p>If I leave it the way it is above when I try and delete the digitalObject I get a foreign key constraint violation.</p>
  
  <p>If I add hasOne mapping on the parent side I get an error saying to specify the other side of the relationship, but I am unclear on where to do that as a DigitalObject can belong to either an Image or Video. Like such.</p>
  
  <pre><code>class Video {
     static hasOne = [englishVersion: DigitalObject, frenchVersion: DigitalObject]
  }
  </code></pre>
  
  <p>Adding belongsTo in the child class results in the same foreign key constraint error. Like such.</p>
  
  <pre><code>class DigitalObject {
    String url
    Date lastUpdated
  
    static belongsTo = [Image, Video]
  }
  </code></pre>
  
  <p>Am I going to have to just give up and handle removing it from its parent manually?</p>
  
  <p>Thanks in advance for any assistance, and sorry if this is answered else ware I was unable to come up with a search query that provides results that deal with a case such as mine although I'm sure this can't be an uncommon use case.</p>
  
  <p><strong>EDIT 24 Jan 2014</strong></p>
  
  <p>Attempting Andrew's suggestion, my actual domain classes are a big more complex than the examples I used therefore my my beforeDelete() ends up like what follows, which is very ugly and could potentially be a pain to maintain.</p>
  
  <pre><code>def beforeDelete() {
      DigitalObject.withNewSession {
          def imageThumbnailImages = Image.findAllByThumbnailImage(this)
          if (imageThumbnailImages) {
              imageThumbnailImages.each { image -&gt;
                  image.thumbnailImage = null
                  image.save(flush: true)
              }
          }
          def imagePreviewImages = Image.findAllByPreviewImage(this)
          if (imagePreviewImages) {
              imagePreviewImages.each { image -&gt;
                  image.previewImage = null
                  image.save(flush: true)
              }
          }
          def imageFullImages = Image.findAllByFullImage(this)
          if (imageFullImages) {
              imageFullImages.each { image -&gt;
                  image.fullImage = null
                  image.save(flush: true)
              }
          }
          def videoThumbnailImages = Video.findAllByThumbnailImage(this)
          if (videoThumbnailImages) {
              videoThumbnailImages.each { image -&gt;
                  image.thumbnailImage = null
                  image.save(flush: true)
              }
          }
          def videoPreviewImages = Video.findAllByPreviewImage(this)
          if (videoPreviewImages) {
              videoPreviewImages.each { image -&gt;
                  image.previewImage = null
                  image.save(flush: true)
              }
          }
          def videoFullVideosEng = Video.findAllByFullVideoEng(this)
          if (videoFullVideosEng) {
              videoFullVideosEng.each { video -&gt;
                  video.fullVideoEng = null
                  video.save(flush: true)
              }
          }
          def videoFullVideosFra = Video.findAllByFullVideoFra(this)
          if (videoFullVideosFra) {
              videoFullVideosFra.each { video -&gt;
                  video.fullVideoFra = null
                  video.save(flush: true)
              }
          }
          def captionsEngVideos = Video.findAllByCaptionsEng(this)
          if (captionsEngVideos) {
              captionsEngVideos.each { videos -&gt;
                  videos.captionsEng = null
                  videos.save(flush: true)
              }
          }
          def captionsFraVideos = Video.findAllByCaptionsFra(this)
          if (captionsFraVideos) {
              captionsFraVideos.each { video -&gt;
                  video.captionsFra = null
                  video.save(flush: true)
              }
          }
          def signLanguageEngVideos = Video.findAllBySignLanguageEng(this)
          if (signLanguageEngVideos) {
              signLanguageEngVideos.each { video -&gt;
                  video.signLanguageEng = null
                  video.save(flush: true)
              }
          }
          def signLanguageFraVideos = Video.findAllBySignLanguageEng(this)
          if (signLanguageFraVideos) {
              signLanguageFraVideos.each { video -&gt;
                  video.signLanguageFra = null
                  video.save(flush: true)
              }
          }
      }
  </code></pre>