Making the id field start at 1000 instead of 1 in a Grails domain class

Recently I wanted to make a domain class in a grails app and have the built in id field not start at 1. I wanted it to start at 1000. This was for a generic “Device” class I plan on extending with specific devices like Virtualmachine and Hostsystem devices. I wanted these devices to have numbers starting with 1000 for no reason other than ‘just because’. I did some searches but couldnt find many simple explanations so once I got a working solution I thought I would share what I used.

Ive been working on a tool I will be talking about in future posts, and will only talk about in this post for the code used to make our id generator work. If you look at my Device.groovy you will see exactly what to do.


static mapping = {
id generator: "org.hibernate.id.enhanced.SequenceStyleGenerator", params: [initial_value:1000, increment_size:1]
}

Here I select the type of generator to use, then in the params I set an initial_value of 1000 You can set this to be anything, next I tell it to increment by 1. I found this documentation helpful in my quest: http://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/id/enhanced/SequenceStyleGenerator.html I hope you find this post helpful.