1

I have a class as:

class Facebook {

   long id
   String username
   String email
   String first_name
   String last_name
   Date birthday
   String gender
   String link
   static hasMany = [friends: FacebookFriend]

   static constraints = {
      id generator:'assigned'
      birthday blank:false, nullable:true
      gender blank:false, nullable:true
   }
}

Based on the documentation for Hibernate and Grails id generator:'assigned' is all I need to be able to set the id manually. However, every time I run the code the ID gets overwritten by GORM, even though I manually set it before calling the .save(flush:true, failOnError:true) method. When I output the id from the object before and after save this is what I get:

ID before save is: 12345645

ID after save is: 1

I'm new to Grails but based on all I've read it seems I'm doing what I'm supposed to. Can anyone provide some insight? Thanks!

My next step is to run the app on Grails 2.3.4 just in case.

Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
Lostlinkpr
  • 1,453
  • 2
  • 12
  • 13

1 Answers1

4

The generator property goes in the mapping block, not constraints:

static constraints = {
   birthday blank:false, nullable:true
   gender blank:false, nullable:true
}

static mapping = {
   id generator: 'assigned'
}
Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156