0

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!

lwi
  • 1,682
  • 12
  • 21
  • 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 Jun 03 '13 at 13:30
  • If `Artefact` is a domain object (table per subclass strategy), can you try using `def task = Artefact.get(id)` in the former case. – dmahapatro Jun 03 '13 at 13:33
  • Just an FYI - `blank:false` is doing nothing for you. – James Kleeh Jun 03 '13 at 14:24

2 Answers2

0

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.

sbglasius
  • 3,104
  • 20
  • 28
0

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.

Community
  • 1
  • 1
osborp
  • 362
  • 1
  • 3
  • 9