Search code examples
grailspersistencegrails-orm

Grails using auto id to create additional property at domain persistence


There's surely a better way?

I want to use the auto generated id to create another custom id. Current I'm using the afterInsert method below...

def afterInsert() {
    def Instance = TttTicket.get(this.id)
    Instance.number = project.ticketPrepender + id.toString().padLeft( 5, '0' )
    Instance.save()
}

Can anyone suggest a better way?


Solution

  • Can anyone suggest a better way?

    Yes.

    The best thing to do depends on some factors like why this property exists, will it ever change and where do you need access to it.

    One option is to make the number transient, remove the number field and have a getNumber() method which returns something like project.ticketPrepender + id.toString().padLeft( 5, '0' ).