Search code examples
grailsgrails-orm

Custom 16 digit ID Generator in Grails Domain


Instead of a sequence of ID which GORM creates, I want to use a 16 digit random number as ID for all the tables GORM creates. I need help as to how to do that. I tried doing

static mapping = { id generator: 'uuid2' }

it didnt work. Help appreciated


Solution

  • You can use a simple mapping like:

    class Book {
        static mapping = {
            id generator:'assigned'
         }
        def beforeInsert() {
            id = YourRandomGenerator.nextInt()
        }
    }
    

    Note the use of the beforeInsert event to assign the id. Greetings.