1

I am using my custom auto increment key in my domain class using

 static mapping = { id generator: 'increment', name: 'personId' }

Is it possible to start the auto increment from a particular value , say start from 100 ?

dev
  • 1,085
  • 4
  • 19
  • 26
  • In domain model object's you don't need id field. Or you have some interesting task? And why do you want set particular value for id? Id field must be unique and it doesn't metter 100 or 1000 is first id. I think it depends on your data base. – alnasfire Mar 26 '15 at 14:48
  • Why don't you do this in your custom id generator? – Andriy Budzinskyy Mar 26 '15 at 14:49
  • @alnasfire my problem is i am not providing value while inserting data to this domain.I use personId as the auto increment value crated by db but , I need it to start from a 3 digit number – dev Mar 26 '15 at 14:53
  • @dev, wich data base do you use? – alnasfire Mar 26 '15 at 14:56
  • 2
    @dev, look at this http://stackoverflow.com/questions/1485668/how-to-set-initial-value-and-auto-increment-in-mysql – alnasfire Mar 26 '15 at 14:58
  • so , do I need to manually execute the query `ALTER TABLE users AUTO_INCREMENT=100` , or can I set it from domain class – dev Mar 26 '15 at 15:01
  • It will need to be done manually. Well, It will have to be done from the database prompt. – huwman Mar 26 '15 at 15:02
  • @AndriyBudzinskyy how do I do it in custom id generator? – dev Mar 26 '15 at 15:04
  • @dev, maybe it will help you http://grails.1312388.n4.nabble.com/Setting-IDs-manually-handling-quot-gaps-quot-in-the-ID-series-td4650976.html – alnasfire Mar 26 '15 at 15:08

1 Answers1

0

You can set the initial value of the autoincrement by using the org.hibernate.id.enhanced.SequenceStyleGenerator generator. This generator takes the parameter initial_value, which will be the the value of your first id. (And after that, it will increment by 1, just like you are used to.)

static mapping = {
    id(generator: 'org.hibernate.id.enhanced.SequenceStyleGenerator', 
        params: [sequence_name: 'start_seq', initial_value: 100])
}
Ivar
  • 6,138
  • 12
  • 49
  • 61