Mitteg
Mitteg

Reputation: 169

Grails static mapping fails (repeated id)

I'm trying to do reverse engineering and create a model class for a given table schema in a database.

The table's name is infopac_usersProva and it has two columns:

I have written the model like this:

    class Infopac_usersProva {

       String strCip 
       String usernm

       static mapping={
        datasource 'gpaq'
        table 'infopac_usersProva'
        version false
        columns{
            id column: 'strCip'
            usernm column: 'USERNM', sqlType: "varchar(75)"
            strCip column: 'strCip', sqlType: "varchar(15)"
        }
       }

       static constraints = {
        strCip (nullable:true, insert:false, update:false)
       }
    }

But I get this error:

Repeated column in mapping for entity: edu.upc.gpaq.domain.generic.Infopac_usersProva column: strCip (should be mapped with insert="false" update="false")

I need to specify the column name for strCip because if I take out that line the model is trying to fetch str_cip instead of strCip. And if I take out "id column: 'strCip' then I get an error saying that there is no id column.

What am I doing wrong?

Upvotes: 0

Views: 3313

Answers (4)

Mitteg
Mitteg

Reputation: 169

I ended up doing the following (see the bold text)

class Infopac_usersProva {

  String usernm
  String id

  static mapping={
    datasource 'gpaq'
    table 'infopac_usersProva'
    version false
    autoTimestamp false
    columns{
        **id column: 'strCip', sqlType: "varchar(15)"**
        usernm column: 'USERNM', sqlType: "varchar(75)"
    }
  }

  static constraints = {

  }
}

It works now. Thank you a lot!

Upvotes: 0

Jim Sosa
Jim Sosa

Reputation: 628

Might be a little late for this, but you need to use the updateable and insertable properties instead. It worked for me:

class Infopac_usersProva {

   String strCip 
   String usernm

   static mapping={
    datasource 'gpaq'
    table 'infopac_usersProva'
    version false
    columns{
        id column: 'strCip'
        usernm column: 'USERNM', sqlType: "varchar(75)"
        strCip column: 'strCip', updateable: false, insertable: false
    }
   }
}

Upvotes: 0

jaetzold
jaetzold

Reputation: 1688

This one should work:

class Infopack_usersProva {
    String strCip
    String usernm

    static constraints = {
        strCip(nullable: false, maxSize: 15)
        usernm(nullable: true, maxSize: 75)
    }

    static mapping = {
        datasource('gpaq')
        table('infopac_usersProva')
        version(false)
        autoTimestamp(false)
        usernm(column: 'USERNM')
        strCip(column: 'strCip')
        id(column: 'strCip', name: 'strCip', generator: 'assigned')
    }
}

But it has strCip as not null. But AFAIK you need an id column that is not null so i do not see any way around this. At least with grails/hibernate.

And if you want strCip never be included in any save() you need to specify

strCip(column: 'strCip', insertable: false, updateable: false)

in the mappings block. I am not aware of any constraints called insertor update and would expect them to just get ignored there.

Upvotes: 0

Ofir Winegarten
Ofir Winegarten

Reputation: 9365

I think that you can get rid of strCip definition.
Instead define the id field properly.

See if this works for you:

class Infopac_usersProva {

String usernm

static mapping={
    datasource 'gpaq'
    table 'infopac_usersProva'
    version false
    columns{
        id generator: 'assigned', name: 'strCip', type: 'string'
        usernm column: 'USERNM', sqlType: "varchar(75)"
    }
}

I didn't check this...

Upvotes: 1

Related Questions