Often as I add a helper method to a domain object I get an error when compiling which resolves to “x property is not found”. This seems to happen for methods name getX, setX, and also recently isX. Is there a list of name forms that I should avoid? Is there a way to annotate or otherwise label these methods so Grails doesn’t confuse them with auto properties?
Often as I add a helper method to a domain object I get an
Share
Grails autodetects properties and assumes that they’re persistent. Public fields in Groovy create a getter and setter under the hood so getters are assumed to be associated with persistent fields.
But if you want a helper method that starts with ‘get’ or ‘is’ but isn’t a getter for a persistent field, you have two options. One is to use the
transientslist – see http://grails.org/doc/latest/ref/Domain%20Classes/transients.htmlThe other option is to declare the return value as
def. Since it’s not typed (def is an alias for Object) Hibernate can’t persist it since it doesn’t know what data type to use, so it’s ignored.My preference is the
transientslist because I would rather have self-documenting methods where it’s obvious what they do, what parameter types they accept, and what they return.