gpt4 book ai didi

grails - 与Grails hasMany和belongsTo创建双向关系不起作用

转载 作者:行者123 更新时间:2023-12-02 15:53:48 24 4
gpt4 key购买 nike

我正在尝试在Grails(父子)的两个域之间创建双向关系,但是我似乎无法使其正常工作。
根据Grails GORM documentation oneToMany的说明,我应该能够在 parent 与 child 之间创建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
}

在对Game域进行更改之后,我继续运行 grails dbm-gorm-diff fileupdated.groovy,以便最终创建双向关系,但得到一个空的迁移文件
databaseChangeLog = {
}

注意:即使将“hasMany”放在游戏域的第一次迁移中,我也会收到相同的结果,但仍会创建子 parent (游戏评论)关系,但父子(游戏要评论)关系不会创建。在教程中,我试图按照它的要求工作,但我使用的是grails 2.4.4。

为什么未创建oneToMany关系?

谢谢,

埃克斯

编辑:

-我创建一个如下的游戏示例,并接收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

-现在,如果我的双向域一切都正确完成了,我应该可以查询游戏中的所有评论,但是我得到的是null
    groovy:000> retrieve = com.pluralsight.Game.get(1)
===> com.pluralsight.Game : 1
groovy:000> retrieve.reviews
===> null

因此,我不确定为什么我的从游戏到评论的oneToMany无法正常工作

最佳答案

您为什么期望数据库更改?

重要的是要在谈论双向关系时了解Grails / GORM(和Hibernate)的含义。

当关系的两端都引用另一侧时,该关系称为双向。因此,Game具有Reviews的集合,并且Review具有对Game的引用。这只是在谈论应用程序代码。数据库只需要1个外键即可完全存储关系。

您也可以使用联接表,但是在这种情况下(我认为),这确实过高了。但是Grails / GORM允许这样做,只需在docs的static mapping部分中查看如何映射它即可。

关于grails - 与Grails hasMany和belongsTo创建双向关系不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35532836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com