Grails域类不添加集合对象到一个到多个映射类Grails 5.1.7



我在一个课程创建者网站上工作,一个track可以有多个courses,反过来,他们可以有多个assignments

域类定义如下:

class Track {
String name
static hasMany = [courses: Course]
static constraints = {
name minSize: 2
courses nullable: true
}
@Override
String toString() {
return name
}
}

class Course {
String name
static hasMany = [assignments: Assignment]
static belongsTo = Track
static constraints = {
name nullable: false
assignments nullable: true
}
@Override
String toString() {
return name
}
}

class Assignment {
String name
String description
String url
static belongsTo = Course
static constraints = {
name nullable: false
description minSize: 20, widget: 'textarea'
url nullable: true, url: true
}
@Override
String toString() {
return name
}
}

而最终目标是建立多对多关系。

引导。Groovy文件包含以下代码:

Assignment assignment1 = new Assignment(name: "Read Software Craftsman", description: "Software Craftsman " +
"by Sandro Mancuso is one of the mandatory readings at Incubyte",
url: "https://www.amazon.in/s?k=software+craftsmanship&sprefix=software+craftsman%2Caps%2C231&ref=nb_sb_ss_ts-doa-p_1_18")
Assignment assignment2 = new Assignment(name: "Read Clean Coder", description: "Principles of clean coding is vital for the code readability. ",
url: "https://www.amazon.in/Clean-Coder-Robert-C-Martin/dp/813178696X/ref=sr_1_1?crid=187IC46P73OZ7&keywords=clean+coder&qid=1654424906&sprefix=clean+coder%2Caps%2C451&sr=8-1")
Course course = new Course(name: "Values and Practices of Software Development")
Track track = new Track(name: "Practices").addToCourses(course)
course.addToAssignments(assignment1).addToAssignments(assignment2)
track.save()
course.save()
assignment1.save()
assignment2.save()
println track.getErrors()

当每个域对象都被成功保存时,一对多的关系没有被持久化,因此映射表TRACK_COURSE和COURSE_ASSIGNMENT没有数据。

当每个域对象都被成功保存时对于许多关系没有被持久化,因此映射表TRACK_COURSE和COURSE_ASSIGNMENT没有数据

我无法复制,但https://github.com/jeffbrown/sapgormrelationships的项目可能会帮助你。

我已经将您的代码直接从问题粘贴到该项目中。在启动时,您将看到将以下内容写入标准输出:

Hibernate: insert into track (id, version, name) values (default, ?, ?)
Hibernate: insert into course (id, version, name) values (default, ?, ?)
Hibernate: insert into assignment (id, version, url, name, description) values (default, ?, ?, ?, ?)
Hibernate: insert into assignment (id, version, url, name, description) values (default, ?, ?, ?, ?)
org.grails.datastore.mapping.validation.ValidationErrors: 0 errors
Hibernate: insert into track_course (track_courses_id, course_id) values (?, ?)
Hibernate: insert into course_assignment (course_assignments_id, assignment_id) values (?, ?)
Hibernate: insert into course_assignment (course_assignments_id, assignment_id) values (?, ?)

您可以看到插入到course_assignmenttrack_course中。你还可以通过与应用程序中的默认脚手架页面交互来验证数据是否存在。

grails-app/init/sapgormrelationships/中# L14-L30

@Transactional
void populateData() {
Assignment assignment1 = new Assignment(name: "Read Software Craftsman", description: "Software Craftsman " +
"by Sandro Mancuso is one of the mandatory readings at Incubyte",
url: "https://www.amazon.in/s?k=software+craftsmanship&sprefix=software+craftsman%2Caps%2C231&ref=nb_sb_ss_ts-doa-p_1_18")
Assignment assignment2 = new Assignment(name: "Read Clean Coder", description: "Principles of clean coding is vital for the code readability. ",
url: "https://www.amazon.in/Clean-Coder-Robert-C-Martin/dp/813178696X/ref=sr_1_1?crid=187IC46P73OZ7&keywords=clean+coder&qid=1654424906&sprefix=clean+coder%2Caps%2C451&sr=8-1")
Course course = new Course(name: "Values and Practices of Software Development")
Track track = new Track(name: "Practices").addToCourses(course)
course.addToAssignments(assignment1).addToAssignments(assignment2)
track.save()
course.save()
assignment1.save()
assignment2.save()
println track.getErrors()
}

您没有提供足够的信息来确定,但我怀疑事务没有得到适当的管理。如果您从我的示例中删除@Transactional,我希望您可以看到与上面报告的相同的行为。

相关内容

  • 没有找到相关文章

最新更新