John Giotta
John Giotta

Reputation: 16934

Additional Datasource with Difference Table Name

I have a domain called Test that has been running look-up SQL queries to select from another database.

I want to get away from this implementation and goto multiple datasource support of Grails 2.0. However, the table name in the other database is a called Panel.

Is it possible to map the domain to the alternative database and also map which table it selects from?

// Datasource.groovy
development {
    dataSource {
        dbCreate = 'create-drop' // one of 'create', 'create-drop','update'
        url = "jdbc:sqlserver://machine\\SQLEXPRESS;databaseName=primarydb"
        username = "user"
        password = "password"
    }
    dataSource_otherdb {
        url = "jdbc:sqlserver://remoteserver:1433;databaseName=otherdb"
    }
}

// Test.groovy
class Test {

    String name
    int key
    String abbreviation

    boolean active = true

    static mapping = {
        sort name: "asc"
        datasource("otherdb")
    }
}

Upvotes: 0

Views: 92

Answers (1)

coderLMN
coderLMN

Reputation: 3076

There is a "table"configuration in mapping closure for that.

// Test.groovy
class Test {

    String name
    int key
    String abbreviation

    boolean active = true

    static mapping = {
        table 'Panel'        //customize table name
        sort name: "asc"
        datasource("otherdb")
    }
}

Upvotes: 2

Related Questions