4
votes

I'm trying to get a project up and running using Grails. I have 2 tables, areaserver and serverprotocol. areaserver has a one-to-many relationship with serverprotocol (one areaserver to many serverprotocols). The entity class AreaServer has an assigned string primary key. I've manually configured the AreaServer's id key to be an assigned string, and I've configured the ServerProtocol class as the many in the AreaServer's many-to-one relationship, but when I try and set the foreign key manually in AreaServer's "mapping" section, Grails uses the conventional approach where it assumes AreaServer's foreign key is an auto-incrementing id with value "id"

Heres my AreaServer entity class:

class AreaServer {


String serverId
...
...
static hasMany = [ serverProtocol : ServerProtocol ]

static mapping = {          
        serverProtocol lazy:false           
        id name: 'serverId', generator: 'assigned'
        serverProtocol column: 'server_serverId'              
}

}

Here is my ServerProtocol entity class:

class ServerProtocol {

long dbId
....
....
static belongsTo = [ areaServer : AreaServer ]

static mapping = {

     id name: 'dbId'
     version false
}

}

When Grails creates the database and tables the "serverprotocol" table has a foreign key that is named "areaServer_id" instead of "server_serverId" which I configured it to do in AreaServer's mapping section. Configuring the foreign key like I am attempting to do is supposed to be a standard procedure as shown under "table and name columns" in the Grails.org document http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20%28GORM%29.html For some reason altering the foreign key name (when the foreign key is nested within the "many" table of the one-to-many relationship) just doesn't work, which the documentation says that it should.

I know that Grails standard convention is to expect each table to have an auto-incrementing primary key called "id", and that is why the foreign key is being named "areaServer_id" (since AreaServer is referenced as areaServer in ServerProtocol). Does anyone know why Grails wouldnt allow me to modify the foreign key's name manually like it says is possible in the Grails's documentation?

Thank you for any help in advance!

1

1 Answers

0
votes

Something like this may work I think:

class AreaServer {
    static hasMany = [serverProtocol: ServerProtocol]
    static mappedBy = [serverProtocol: "server"]
}

class ServerProtocol {
    static mapping = { server column: 'server_serverId'  }

    AreaServer server
}