在Grails中使用组合键和不同列名的一对多 [英] One-to-Many With Composite Keys and Different Column Names in Grails
问题描述
这是一个语法问题。我想要Foo - > Bar(简化)之间的一对多关系:
class Foo {
串fooPK1,fooPK2
静态映射= {
ID复合:[ fooPK1, fooPK2]
}
静态的hasMany = [巴:酒吧]
}
类酒吧{
字符串fooPK1,dumbNameForFooPK2,barPK1,barPK2
富myFoo
静态映射= {
id复合:[barPK1,barPK2]
列{
myFoo [:] {
列名:FOO_PK_1
列名:?? ???????????
$ b 在这种情况下,显然Foo.fooPK1映射到Bar.fooPK1,但我需要Foo.fooPK2映射到Bar.dumbNameForFooPK2。我希望这是有道理的。
我的问题是我不知道语法应该是什么(或者是否有更好的方法来做到这一点!可以发现,grails文档并没有真正的帮助。
解决方案你需要重命名Bar中的外键列声明,wright ?
class Bar {
Foo myFoo
static mapping = {
列{
myFoo {
//按照您的id组合顺序声明它们。
列名: foo_pk_1
列名: dumb_name_for_foo_pk_2
}
}
}
}
This is a syntax question. I want a one-to-many relationship between Foo -> Bar (simplified here):
class Foo {
String fooPK1, fooPK2
static mapping = {
id composite: ["fooPK1", "fooPK2"]
}
static hasMany = [bars: Bar]
}
class Bar {
String fooPK1, dumbNameForFooPK2, barPK1, barPK2
Foo myFoo
static mapping = {
id composite: ["barPK1", "barPK2"]
columns {
myFoo[:] {
column name: "FOO_PK_1"
column name: "?????????????"
}
}
}
}
In this case, obviously Foo.fooPK1 maps to Bar.fooPK1, but i need Foo.fooPK2 to map to Bar.dumbNameForFooPK2. Hopefully this makes sense.
My problem is I have no idea what the syntax is supposed to be (or if there's a better way to do this!) and from what I could find, the grails documentation wasn't really helpful.
解决方案 You need to rename the foreign key columns declaration inside Bar, wright?
class Bar {
Foo myFoo
static mapping = {
columns {
myFoo {
//declare them in the order of your id composite.
column name: "foo_pk_1"
column name: "dumb_name_for_foo_pk_2"
}
}
}
}
这篇关于在Grails中使用组合键和不同列名的一对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!