好的,我迷路了......我有一个域类Student
和Course
。类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
创建一个单独的域类并映射Student
和Course
个StudentCourse
个班级Content-Type
。这是对的吗?
答案 0 :(得分:0)
一般来说,在处理多对多关联时,您需要
parent.addTo*(child)
将实例添加到集合中,然后调用parent.save()
来保留它们。 key
而不是column
。 此外,我建议在调用org.springframework.transaction.annotation.Transactional
的控制器方法上使用save()
注释,以便您不需要显式刷新。在您的情况下,Student
是该关联的所有者(因为课程设置为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的更多信息,请创建一个新问题。