Search code examples
grailsgrails-orm

Grails: Connect to legacy database


I want to connect to the legacy database. My legacy database is using postgresql. I use db-reverse-engineer plugin to generate the domain class, the generated domain class :

class TableLogin {

    String username
    String password
    Integer userLevel

    static mapping = {
        id name: "username", generator: "assigned"
        version false
    }

    static constraints = {
        username maxSize: 30
        password nullable: true, maxSize: 36
        userLevel nullable: true
    }
}

I run generate-all, the action list is work fine. But, when i click one of the data/username (to perform the show action), i got this error : TableLogin not found with id null

And I try to trace with this code :

    def rowLogin = TableLogin.get("supervisor")
    log.error(rowLogin as JSON)

And i got :

{"class":"webnico.TableLogin","id":null,"password":"2dcc509a6f7584","userLevel":0,"username":"supervisor"}

Why the id is null ? I think because the id is null so the show action doesn't work


I update my domain class, become :

class TableLogin {

    String id

    String username
    String password
    Integer userLevel

    static mapping = {
        id name: "username", generator: "assigned"
        version false
    }

    static constraints = {
        username maxSize: 30
        password nullable: true, maxSize: 36
        userLevel nullable: true
    }

    def afterLoad() {
        id = username
    }
}

The id is no longer null

{"class":"webnico.TableLogin","id":"supervisor","password":"2dcc509a6f7584","userLevel":0,"username":"supervisor"}

But, the show action still doesn't work. The show URL seems to be correct, http://mylocalhost.com:9000/webnico/tableLogin/show/supervisor But I still got the same error : TableLogin not found with id null

Is it means we cannot use the scaffold (generate-all) when the id type is not Long ?


Solution

  • Aha... the show action doesn't work properly because by default the parameter of show action is Long. So, we need to modify the parameter type become String.

        def show(Long id) {
    ...
        }
    

    become

        def show(String id) {
    ...
        }
    

    And off course, we need to modify the others action (edit, update, delete) that's need id parameter too.