I was trying to delete a more complex related object from my db, so I investigated using grails console and came up with this:
TextContent textContent = ...The item to delete...
// TextContent hasMany BundleText
// BundleText belongsTo BundleVersion
textContent.bundleTexts.each {
BundleVersion bundleVersion = it.bundleVersion
bundleVersion.removeFromBundleTexts(it)
textContent.removeFromBundleTexts(it)
it.delete()
//bundleVersion.save()
}
// Language hasMany TextContent
// Language belongsTo textContent (?)
textContent.language.removeFromTextContents(textContent)
// TextContent belongsTo textCode
TextCode textCode = textContent.textCode
textCode.removeFromTextContents(textContent)
textContent.delete()
//textCode.save()
Now this all works fine in the grails console, so I put it inside my database service, and run the app. The app runs the method succesfully (verified), but when it re-read the collection it comes up again.. Big mystery...!?
Anyone seen anything like it? Or am I just embarassingly naive?
EDIT
For clarification:
class TextCode {
static hasMany = [ textContents : TextContent ]
}
class TextContent {
Language language
static belongsTo = [ textCode : TextCode ]
static hasMany = [ bundleTexts : BundleText]
}
class BundleText {
TextContent textContent
static belongsTo = [ bundleVersion : BundleVersion ]
}
class Language {
static hasMany = [ textContents : TextContent ]
}
Not sure why the above didn't work, but I managed to solve it by direct HQL instead.
BundleText.executeUpdate("delete BundleText bt where bt.textContent=:content", [content: textContent])
TextContent.executeUpdate("delete TextContent tc where tc.id = :id", [id: textContent.id])