Reputation: 1712
I just encountered a strange behaviour when deleting a object from a hasMany
Relation in grails 2.2.1.
Deleting is not working with:
def lessonInstance = Lesson.get(lessonId)
long id = Long.valueOf(taskId)
def task = Task.get(id)
lessonInstance.removeFromTasks(task)
While deleting is working with:
def lessonInstance = Lesson.get(lessonId)
long id = Long.valueOf(taskId)
def task = lessonInstance.tasks.find { it.id == id }
lessonInstance.removeFromTasks(task)
I expected both to work and I am now curious why the latter works and the first does not work. Here are the Domain classes envolved:
class Lesson{
static hasMany = [tasks:Task]
static hasOne = [skill:Skill]
static constraints = {
tasks(nullable: false, minSize: 1)
skill(nullable: true)
}
}
class Task extends Artefact{
Integer experiencePoints=0
Integer credits=0
static constraints = {
experiencePoints(blank: false, min: 0)
credits(blank: false, min: 0)
}
}
Thanks!
Upvotes: 0
Views: 1884
Reputation: 362
It sounds like the problem might be due to the caching differences between get and find. See Burt Beckwith's answer for Difference between findAll, getAll and list in Grails.
...I tried to replicate the problem using the grails console but both find and get approaches seemed to work.
Upvotes: 0
Reputation: 3124
I think you should go read: http://blog.springsource.org/2010/07/02/gorm-gotchas-part-2/ as it explains why you have to do a little more, to make it work.
Upvotes: 0