AndyRED
AndyRED

Reputation: 361

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?

Upvotes: 0

Views: 55

Answers (1)

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27220

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' ).

Upvotes: 1

Related Questions