如何在grails中保持许多关系的秩序

时间:2014-02-02 12:42:33

标签: grails groovy grails-2.0 grails-domain-class

我的项目要求我在许多关系中维护插入和检索顺序。默认情况下,groovy将元素保存为多对多关系中的Set。我想在列表中这样做。我不知道如何更新关系以使用List而不是Set。任何帮助将不胜感激。

class Course{
        static belongsTo = Teacher
        static hasMany = [teacher:Teacher]

        static mapping = {
            teacher joinTable : [name: TeacherCourse]
        }
    }

    class Teacher{

        static hasMany = [course:Course]
        static mapping = {
            course joinTable : [name: TeacherCourse]
        }
    }
对教师或课程的

save()调用也会在TeacherCourse表中插入一个新行。它没有任何问题。在数据库中有表格: - 老师(PK:Id) 课程(PK:Id) TeacherCourse(PK:[Teacher_id,Course_id])

有没有办法可以在许多关系中保持插入和检索的顺序?

谢谢..

修改 的 在控制器save()

def courseInstance = new Course()
List <Teacher> teacherList= []
teacherList.add(Teacher.findById(65))
teacherList.add(Teacher.findById(36))
courseInstance.courseUnits = teacherList
courseInstance.save(flush:true)

1 个答案:

答案 0 :(得分:1)

试试这个:

class Course {
    List teachers

    static belongsTo = Teacher
    static hasMany = [teachers:Teacher]

    static mapping = {
        teachers joinTable : [name: TeacherCourse]
    }
}

class Teacher {
    List courses

    static hasMany = [courses:Course]
    static mapping = {
        courses joinTable : [name: TeacherCourse]
    }
}

Reference