【问题标题】:Grails - Many-to-Many Data persistance (EDITED)Grails - 多对多数据持久性(已编辑)
【发布时间】:2015-12-16 19:07:24
【问题描述】:

好吧,我迷路了...我有一个域类StudentCourse。类Course 已设置。我没有在表格中添加任何新课程。一个学生可以有很多课程,一个课程可以有很多学生。请看看我有什么,并指导我正确的方式。

class Student {
    String fullName

    static belongsTo = [schools: School]
    static hasMany = [courses:Course]

    static mapping = {
        table "STUDENT"
        courses joinTable: "STUDENT_COURSE", key: "COURSE_ID"  
}

class Course {
    String courseName   

    static hasMany = [students:Student]
    static belongsTo = Student

    static mapping = {
        students joinTable: "STUDENT_COURSE", key: "STUDENT_ID"
}

现在,当我输入新的学生信息时,视图不会爆炸,这很好......但是它保存了学生信息,但 joinTable STUDENT_COURSE 是空白的。我知道不知何故我需要将 studnet 的 PK ID 和课程传递给 joinTable,但我不知道如何以及在哪里。 (它在def save()下的studentController中吗?) 另外,.gsp 应该是什么样子?这就是我所拥有的,我知道我错了。 (id="coursetags" is jQuery autocomplete id)

我的_form.gsp 输入部分。

<div class="fieldcontain ${hasErrors(bean: studentInstance, field: 'courses', 'error')} required" >
<label for="course">
    <g:message code="student.courses.label" default="Course" />
    <span class="required-indicator">*</span>
</label>
<g:textField name="course" id="coursetags" required="" value="${course?.course}"/>

我的结果应该是这样的。

STUDENT             COURSE                      STUDENT_COURSE
ID      FULLNAME    ID          COURSENAME      STUDENT_ID   COURSE_ID
1       John Doe    1           English101        1             1
2       Jane Smith  2           Science101        1             2
                                                  2             2

我一直在尝试分析这些网站...

http://chrisbroadfoot.id.au/2008/07/19/many-to-many-relationship-mapping-with-gorm-grails/

https://grails.github.io/grails-doc/3.0.x/guide/GORM.html#manyToMany

https://grails.org/wiki/Many-to-Many%20Mapping%20without%20Hibernate%20XML

谢谢。

编辑 1

我的控制器

class studentController {
    @Transactional
    def save(Student studentInstance) {
        if (studentInstance == null) {
            notFound()
            return
        }

        if (studentInstance.hasErrors()) {
            respond studentInstance.errors, view:'create'
            return
        }  

    def courseID = Course.findByCourseLike(params.course)

    studnetInstance.save flush:true

    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.created.message', args: [message(code: 'student.label', default: 'Student'), studnetInstance.id])
            redirect studentInstance
        }
        '*' { respond studentInstance, [status: CREATED] }
    }

    def sID = studentInstance.id    //When I println under these, they print the right id numbers.
    def cID = courseID.id

    student.addToCourses(cID).save()  //This is the part that I don't understand.
                                     //I get error saying No such property: student for class.

    }
}

编辑 2 所以,我在考虑并做一些研究....除非我直接使用 SQL 到STUDENT_COURSE 连接表,否则我需要为StudentCourse 创建一个单独的域类并将StudentCourse 类映射到@ 987654340@。对吗?

【问题讨论】:

    标签: grails many-to-many persistence grails-orm grails-2.4


    【解决方案1】:

    一般来说,在处理多对多关联时,您需要

    1. 调用parent.addTo*(child) 将实例添加到集合中,然后调用parent.save() 将它们持久化。
    2. 在两个 joinTable 映射中使用 key 而不是 column

    此外,我建议在调用save() 的控制器方法上使用org.springframework.transaction.annotation.Transactional 注释,这样您就不需要显式刷新。在您的情况下,Student 是关联的所有者(因为 Course 设置为 belongTo Student)。

    import org.springframework.transaction.annotation.Transactional
    
    class StudentController {
    
        @Transactional
        def save() {
            def student = /* some Student instance */
            def course = /* some Course instance */
    
            student.addToCourses(course)
            student.save()
        }
    }
    

    基本上,addTo*() 方法负责连接表。

    普惠制

    这样就可以处理持久性。至于 GSP ......在 StackOverflow 上一次提出多个问题是不受欢迎的。但这是一个非常简单的 HTML/GSP 的总体思路:

    <g:each var="student" in="${students}">
      <g:each var="course" in="${student.courses}">
        <p>${student.id}  ${student.fullName}  ${course.id}  ${course.courseName}</p>
      </g:each>
    </g:each>
    

    有关 GSP 的更多信息,请创建一个新问题。

    【讨论】:

      猜你喜欢
      • 2012-12-14
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 2011-12-06
      • 2013-11-15
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多