开发者

grails joinTable with legacy database

I have a one-to-many table mapping that I want t开发者_Go百科o do with grails. I have a legacy database where I can't change the table structure. I have everything set up, but the only thing I can't figure out is how to get grails to notice the existing foreign keys instead of creating it's own column. I have something like this (simplified):

class Customer {
    String listID
    String name
    String address
    // more fields etc.

    static hasMany [notes : Note]

    static mapping = {
        table name:"customers"
        id name:"listID",generator:"assigned"

        // doesn't work, creates a foreign key column in customer_notes table
        // with key: customer_id. I want it to just use the existing column
        // CustomerListID, which has the correct foreign key
        notes joinTable:[name:"customer_notes",key:"CustomerListID"]            
    }
}

class Note {
    String noteID
    String customerListID

    static mapping = {
        table name:"customer_notes"
        id name:"NoteID",generator:"assigned"
     }
}

As a side note, I see that joinTable in the grails document says "column" is the inverse column, and "key" is the foreign key. The examples in the docs aren't helpful. I have "Grails in Action" and it says it's going to provide a one-to-many example, then shows a many to many example, and either way it doesn't seem to be a complete example (half the model is missing)!

Any ideas? I have this legacy database, and I'm going to use it read-only. I just want the O/R mapper to hook things together nicely.


Your joinTable in the Customer domain object should use column, not key:

notes joinTable:[name:"CustomerNotes",column:"CustomerListID"]

Also, the name: should be the name of the join table, not the name of the Note table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜