0
votes

I have GORM domain class that has a composite key defined in the static mapping like this:

static mapping = {
    table 'myTable'
    id composite: ['Y', 'Z']
    X column: 'xColName'
    Y column: 'yColName'
}

I need to change this class to remove the composite primary key and set the primary key as a generic id field with auto incrementing values. However, there are currently existing rows in table 'myTable' and when I remove the "composite" line from the mapping, Grails add the default 'id', but the default value for 'id' is '0' for every row.

| X     | Y     | id     |
==========================
| a     | d     | 0      |
| b     | e     | 0      |
| c     | f     | 0      |

I have tried many different 'generators' and all types of combinations of id column below:

static mapping = {
   id column: 'id', generator: 'increment'
   ...
}

I have also run the dbm-gorm-diff which generates this:

changeSet(author: " (generated)", id: "1434577127626-10") {
    dropPrimaryKey(tableName: "myTable")
    addPrimaryKey(columnNames: "id", constraintName: "classNamePK", tableName: "myTable")
}

However, by the time the changelog runs GORM has already loaded my domain objects into memory, and has added in the id field with values of all '0'. Is there a way to have myTable have an id column set as the primary key with generated sequence numbers? Or do I need to jump through some hoops by adding these myself and then changing the primary key in the changelog.groovy

| X     | Y     | id     |
==========================
| a     | d     | 1      |
| b     | e     | 2      |
| c     | f     | 3      |
1
What is the database? Table DDL would be helpful as well.Armaiti

1 Answers

0
votes

If you're using an Oracle data source you can create a sequence in the database and specify to use it:

static mapping = {
    id generator: 'sequence', params: [sequence: 'MY_SEQUENCE']
}

Check the Grails documentation for more info on generators, and/or the Hibernate reference page that it links to.