Search code examples
grailsgrails-orm

Grails 2.5 - generate String based on id during insert


I have a field String generated in the domain class, and I'd like to set generated based on the id when the value is inserted. For example, generated might get set to generated100 if the id is 100. I'd ideally like to do this behind the scenes, so calling new DomainClass().save(flush: true) would automatically process and save generated along with the rest of DomainClass. Is this possible to do without turning the save() line into two lines?


Solution

  • I'd highly recommend you read the documentation regarding afterInsert. There you can implement whatever logic you want and have it encapsulated in your domain class.