【发布时间】:2016-02-21 05:58:11
【问题描述】:
我正在尝试在 Grails(父子)中的两个域之间创建双向关系,但我似乎无法使其工作。 根据Grails GORM documentation oneToMany,我应该能够在父母和孩子之间创建一个hasMany(Parent) 和一个belongsTo(Child) 以创建双向关系,但它对我不起作用。
我有以下两个域:
class Game {
String name
String description
Double price
}
class Review {
static belongsTo = [game: Game]
String reviewText
Date reviewDate
}
然后我创建一个grails dbm-gorm-diff file.groovy 我得到一个包含以下内容的文件
databaseChangeLog = {
changeSet(author: "efx (generated)", id: "1456032538941-1") {
createTable(tableName: "game") {
column(name: "id", type: "int8") {
constraints(nullable: "false", primaryKey: "true", primaryKeyName: "gamePK")
}
column(name: "version", type: "int8") {
constraints(nullable: "false")
}
column(name: "description", type: "varchar(255)") {
constraints(nullable: "false")
}
column(name: "name", type: "varchar(255)") {
constraints(nullable: "false")
}
column(name: "price", type: "float8") {
constraints(nullable: "false")
}
column(name: "reviews_id", type: "int8") {
constraints(nullable: "false")
}
}
}
changeSet(author: "efx (generated)", id: "1456032538941-2") {
createTable(tableName: "review") {
column(name: "id", type: "int8") {
constraints(nullable: "false", primaryKey: "true", primaryKeyName: "reviewPK")
}
column(name: "version", type: "int8") {
constraints(nullable: "false")
}
column(name: "review_date", type: "timestamp") {
constraints(nullable: "false")
}
column(name: "review_text", type: "varchar(255)") {
constraints(nullable: "false")
}
}
}
changeSet(author: "efx (generated)", id: "1456032538941-4") {
createSequence(sequenceName: "hibernate_sequence")
}
changeSet(author: "efx (generated)", id: "1456032538941-3") {
addForeignKeyConstraint(baseColumnNames: "reviews_id", baseTableName: "game", constraintName: "FK_jnjkmccicsjmsvqub534xcnnm", deferrable: "false", initiallyDeferred: "false", referencedColumnNames: "id", referencedTableName: "review", referencesUniqueColumn: "false")
}
}
此时一切都“完美”,可以这么说,所以我运行grials dbm-update,因此更改被传输到数据库,但随后我想使这种关系成为双向关系,因此我使用“hasMany”更新我的游戏域'如下
class Game {
static hasMany = [reviews: Review]
String name
String description
Double price
}
对游戏域进行更改后,我继续运行grails dbm-gorm-diff fileupdated.groovy,这样我终于可以创建双向关系,但我得到一个空的迁移文件
databaseChangeLog = {
}
注意:即使将“hasMany”放在 Game 域的第一次迁移中,我也会收到相同的结果,但会创建关系 child-parent(reviews to game),但 parent-没有创建子(游戏到评论)。在我试图遵循的教程中,它确实有效。我使用的是 grails 2.4.4。
为什么没有创建 oneToMany 关系?
谢谢你,
efx
编辑:
-我创建一个如下所示的游戏示例并接收 id = 1
groovy:000> g = new com.pluralsight.Game([description: 'Game Desc', name: 'The Game', price: '1.99'])
===> com.pluralsight.Game : (unsaved)
groovy:000> g.save()
===> com.pluralsight.Game : 1
-然后我继续为该游戏创建 2 条评论并收到评论 id 2 和 3
groovy:000> r = new com.pluralsight.Review([game: g, reviewDate: new Date(), reviewText: 'Review 1'])
===> com.pluralsight.Review : (unsaved)
groovy:000> r.save()
===> com.pluralsight.Review : 2
groovy:000> r2 = new com.pluralsight.Review([game: g, reviewDate: new Date(), reviewText: 'Review 2'])
===> com.pluralsight.Review : (unsaved)
groovy:000> r2.save()
===> com.pluralsight.Review : 3
-现在,如果我的双向域一切正常,我应该能够从游戏中查询我的所有评论,但我得到一个空值
groovy:000> retrieve = com.pluralsight.Game.get(1)
===> com.pluralsight.Game : 1
groovy:000> retrieve.reviews
===> null
因此我不确定为什么我的从游戏到评论的 oneToMany 不起作用
【问题讨论】:
-
您必须从游戏的角度保存您的评论,方法是添加它:
g.addToReviews(new Review(review:date new Date(), reviewText: 'Review 1')。然后当你保存游戏时,评论也会被保存:g.save() -
@EmmanuelRosa 我得到一个错误,我猜想这与我在上面的最后一段代码中尝试从 Game 中检索评论时得到的 null 完全相同。
groovy:000> retrieve.addToReviews(new Review(reviewDate: new Date(), reviewText: 'Review 3')) ERROR org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: groovysh_evaluate: 2: unable to resolve class Review @ line 2, column 23. retrieve.addToReviews(new Review(reviewDate: new Date(), reviewText: 'Review 3')) -
不,问题是在我的示例中,我根本没有使用完全限定的类名。试试:
g.addToReviews(new com.pluralsight.Review(review:date new Date(), reviewText: 'Review 1') -
@EmmanuelRosa 我收到以下错误
groovy:000> retrieve.addToReviews(new com.pluralsight.Review(reviewDate: new Date(), reviewText: 'Review 3')) ERROR groovy.lang.MissingMethodException: No signature of method: com.pluralsight.Game.addToReviews() is applicable for argument types: (com.pluralsight.Review) values: [com.pluralsight.Review : (unsaved)] Possible solutions: getReviews()如果游戏与评论之间没有关系,我认为它永远不会起作用。目前唯一的关系是从评论到游戏,反之则不然 -
是的,你是对的。您需要从
Game到Review的关联。使用我的答案中的任何一个示例。 many 被添加到 one 中,而不管关联是单向的还是双向的。顺便说一句,请随时注册我的 Grails Noob Slack 社区(emmanuelrosa.com)。 StackOverflow 聊天很快就会开始,而且很糟糕。
标签: grails grails-orm