hasMany
从grails 2.2.1 中的关系中删除对象时,我刚刚遇到了一个奇怪的行为。
删除不适用于:
def lessonInstance = Lesson.get(lessonId)
long id = Long.valueOf(taskId)
def task = Task.get(id)
lessonInstance.removeFromTasks(task)
删除正在使用:
def lessonInstance = Lesson.get(lessonId)
long id = Long.valueOf(taskId)
def task = lessonInstance.tasks.find { it.id == id }
lessonInstance.removeFromTasks(task)
我希望两者都能工作,我现在很好奇为什么后者有效而第一个无效。以下是涉及的域类:
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)
}
}
谢谢!