简体   繁体   中英

Grails ID (primary key) column as String

I'm learning Grails and I have a question about legacy tables. Right now I am using scaffolding in my controller, but will eventually (of course) build it out to a custom controller and views - right now I am just trying to get the model right. Say that I have an existing table FOOBAR, with the columns FOOBAR_CODE (varchar, primary key) and FOOBAR_DESC (varchar) - how can I model this (as in make a domain class, such that I can CRUD)? I have scoured the internet to no avail - certainly I am just missing something. Looking at examples, my last attempt is below. When I visit the controller's "list" action I see a "Code" and "Desc" column, the "Desc" column has values in it but the "Code" column doesn't (I have my application hooked-up to an existing table with some rows in it) - so clearly my app isn't associating my "Code" property/column with the correct column (FOOBAR_CODE) - which is further evidenced by the fact that when when I try to add a new entry and I get the error "Field 'FOOBAR_CODE' doesn't have a default value" - the app isn't associating the "Desc" field on the create form with the FOOBAR_DESC column in the DB. I am sure that my question will be answered in my future studies, I know I'm jumping the gun a bit. Does anybody have any suggestions?

I am sure that one of my problems is the "generator: 'assigned'" - which seems to be relying on the DB to assign a value.

Basically, I have an existing table with two varchar columns (one is the primary key) and I am attempting CRUD capabilities.

I would greatly appreciate any insight!

Thanks!

class FOOBAR {

    String id
    String code
    String desc

    static constraints = {
    }

    static mapping = {
        table "FOOBAR"

        version false
        columns {
            id column: 'FOOBAR_CODE', generator: 'assigned', name: 'code'
            desc column: "FOOBAR_DESC"
        }
      }


}

Try this:

class FOOBAR {

    String code
    String desc

    static constraints = {
    }

    static mapping = {
        table "FOOBAR" //schema: "SCHEMA" if it is different from the user

        version false
        id generator: 'assigned', name: 'code'
        code column: "FOOBAR_CODE"
        desc column: "FOOBAR_DESC"
      }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM