How do I create a transient variable in a Grails Domain Class?

coderberry picture coderberry · Dec 16, 2011 · Viewed 11.3k times · Source

How do I set up a variable within a domain class that is not persistent. I want to be able to write and read to that variable, but I don't want it to be a part of the table.

The way to do this in rails is by setting up a variable with attr_accessor. Is this possible in Grails? Does anyone know how to do this?

Thanks!

Answer

Dónal picture Dónal · Dec 16, 2011

Just add the names of all the transient variables to the transients list, e.g.

class MyDomain {

  static transients = ['nonPersistent', 'nonPersistent2']

  Integer nonPersistent
  Integer nonPersistent2

  Integer persistent
  Integer persistent2      
}