Reputation: 1016
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?
Upvotes: 0
Views: 730
Reputation: 27200
Is there any good reason for that?
Yes. This has been the behavior since Grails 2.0.2. Properties which are not bindable by default are those related to transient fields, dynamically typed properties and static properties. There is some discussion about this at https://spring.io/blog/2012/03/28/secure-data-binding-with-grails.
Grails domain instantiation overrides the default Groovy behaviour?
Yes. This allows for doing a number of things which are common in a web app, like binding request parameters to a domain instance in a way that allows for a lot of flexibility needed to bind a bunch of Strings (request parameters) to a graph of objects.
If you really want to bind to a transient property all you have to do is configure the property to be bindable:
class Address {
String street
String number
static transients = ["number"]
static constraints = {
number bindable: true
}
}
See http://grails.github.io/grails-doc/2.4.5/ref/Constraints/bindable.html.
I hope that helps.
Upvotes: 1
Reputation: 1749
You can do it by two-way.
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']
}
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.
Upvotes: 2