Search code examples
grailsgrails-ormgrails-domain-class

Grails strange removeFrom* behavior


I just encountered a strange behaviour when deleting a object from a hasManyRelation 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!


Solution

  • 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.