Bosh
Bosh

Reputation: 8748

Can I rename GORM's "version" field? (Grails 2.2 + GORM MongoDB)

I've got a domain object that already has a property called versions, so I'd like to give a different name to the built-in version property (used in GORM for optimistic locking). For instance, I'd like to call it updateCount instead.

Note that I do want the semantics of optimistic locking; I just want to give the field another name. Here's what I've naively tried (and it didn't work):

class Item {
    ObjectId id
    static hasMany = [versions: ItemVersion]
    static mapping = {
        table 'item'
        version column: 'updateCount'  //  <-- This was my attempt
    }
}

I would definitely appreciate any help in...

  1. Determining whether this is possible, and
  2. If so, making it work :-)

Thanks!

Upvotes: 2

Views: 1003

Answers (1)

dmahapatro
dmahapatro

Reputation: 50265

First thing first. MongoDB (NoSQL) deals with Documents and Collections instead of Table and rows.

Being said that, the domain class should look like:

class Item {
    ObjectId id
    String itemName

    static hasMany = [versions: ItemVersion]
    static mapping = {
        //Collection in Mongodb is to Table in relational world
        collection 'item'

        //attr in Mongodb is to column in relational world
        itemName attr: 'item_name'

        //After spending some time investigating it was found that
        //attr for version does not make any difference
        //The below would not work for implicit GORM variable "version"
        //default attribute name is the variable name.
        //version attr: 'updateCount' 

    }
}

In case you want to configure default property across the domains to switch on/off the versioning then have a look at Global Mapping Configuration.

Upvotes: 4

Related Questions