Search code examples
grailsgroovygrails-orm

Domain class constructors in Grails?


I have some code that I want to run when a domain class object is created; in Java, I would include this code on the constructor. How can I do it in Groovy/Grails?

Thanks.


Solution

  • Have you seen this page about groovy constructors? I have had success adding map constructors to Grails domain classes using this technique.

    This article contains a good example and highlights an important issue. If you want to disable the map constructor for a Grails domain class (not that I think that's a particularly good idea), you might try throwing a runtime exception rather than returning a new instance. Or, have your map constructor marshall the data and call one of your other constructors.