Search code examples
grailsgrails-orm

Missing transient properties on Domain constructor (with mapped parameters)


Using the map automatic creation on domain classes doesn't fill in transient properties:

class Address {
  String street
  String number
  static transients = ["number"]
}

def address = new Address(street: "King's Street", number: "23")
println address.street //King's Street
println address.number //null

Is there any good reason for that? Grails domain instantiation overrides the default Groovy behaviour?


Solution

  • You can do it by two-way.

    1. If you want to make transient a field, you need to bind it.

      class Address {
      String street
      String number
      
      static constraints = {
          number bindable: true, nullable:true
      }
      static transients = ['number']
      }
      
    2. You can bind it using some getter method.

      class Address {
      
      String street
      String number
      
      String getDifferentNumber() { number }
      
      static transients = ['differentNumber']
      }
      

    Hope it will help you. Enjoy.