One-to-Many With Composite Keys and Different Column Names in Grails

谁都会走 提交于 2019-12-12 04:37:05

问题


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.


回答1:


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"
      }
    }
  }

}


来源:https://stackoverflow.com/questions/16487271/one-to-many-with-composite-keys-and-different-column-names-in-grails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!