15

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!

Dónal
  • 185,044
  • 174
  • 569
  • 824
coderberry
  • 3,040
  • 3
  • 26
  • 28

3 Answers3

26

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      
}
Dónal
  • 185,044
  • 174
  • 569
  • 824
1

Defines a list of property names that should not be persisted to the database. This is often useful if you have read-only accessor methods ("getters") that are helper methods but get confused as being persistence-related.

Examples

class Author {
   String name
   String getUpperCaseName() { name.toUpperCase() }
   static transients = ['upperCaseName']
}
Om Prakash
  • 793
  • 7
  • 14
0

Here I have created transient variable in domain class-

class Application {
    dataType domainFields  //define datatypes
    static transients = [ 'name']

    String  getName() {
        return 'grails App'
    }
}
Emdadul Sawon
  • 5,730
  • 3
  • 45
  • 48
R Tiwari
  • 325
  • 3
  • 9