This is ugly and probably not the best method for doing this but as I could not get the other methods suggested to work reliably, and due to the fact that I wanted to make the minimum changes as possible to the domain classes as my project is not the only one that uses them.
I added the following method to my DigitalObjectController.
def deleteFromMediaAsset(Long id, String parentClass, String parentProperty, String parentId) {
def digitalObjectInstance = DigitalObject.get(id)
if (!digitalObjectInstance) {
flash.message = message(code: 'default.not.found.message', args: [
message(code: 'digitalObject.label', default: 'DigitalObject'),
id
])
redirect(controller: parentClass, action: 'edit', id: parentId)
return
}
try {
def classOfParent = grailsApplication.domainClasses.find {
it.clazz.simpleName == parentClass.capitalize()
}.clazz
def parentInstance = classOfParent.get(parentId)
parentInstance.setProperty(parentProperty, null)
digitalObjectInstance.delete(flush: true)
flash.message = message(code: 'default.deleted.message', args: [
message(code: 'digitalObject.label', default: 'DigitalObject'),
id
])
redirect(controller: parentClass, action: 'edit', id: parentId)
}
catch (DataIntegrityViolationException e) {
flash.message = message(code: 'default.not.deleted.message', args: [
message(code: 'digitalObject.label', default: 'DigitalObject'),
id
])
redirect(controller: parentClass, action: 'edit', id: parentId)
}
}
Basically I pass the parent Image or Video class and ID along with the attribute to which the DigitalObject refers to and the ID of the DigialObject I wish to delete. I set the property to null on the parent object and then I delete the DigitalObject.
Ugly but it works for now, wish me luck.