0
votes

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!

2
Looks like Grails is not picking up the fact that Task loaded with get() and with Lesson.tasks is the same object. BTW I wonder if Task or Artefact have equals()/hashCode() redefined. - Victor Sergienko
If Artefact is a domain object (table per subclass strategy), can you try using def task = Artefact.get(id) in the former case. - dmahapatro
Just an FYI - blank:false is doing nothing for you. - James Kleeh

2 Answers

0
votes

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.

0
votes

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.