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

3

16

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!

Bechler answered 16/12, 2011 at 15:35 Comment(0)
E
28

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      
}
Electrophorus answered 16/12, 2011 at 15:40 Comment(0)
F
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']
}
Folkways answered 8/3, 2017 at 5:1 Comment(1)
Source: docs.grails.org/3.1.1/ref/Domain%20Classes/transients.htmlDarendaresay
D
0

Here I have created transient variable in domain class-

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

    String  getName() {
        return 'grails App'
    }
}
Directed answered 5/1, 2015 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.