0

I am using a Domain class with a generated identity:

class User {
   ...
   static mapping = {
      id generator: 'assigned', name: 'uid'
   }

   Long uid
   ...
}

but when I try to retrieve an instance using

User.get(1)

it returns null. It does work if I use

User.findByUid(uid)

Is it not possible to use Domain.get or Domain.read when ids are generated?

chozero
  • 411
  • 6
  • 12

2 Answers2

1

Even if your id is assigned, there's no need to declare the attribute.

class User {
  static mapping = {
    id generator: 'assigned' column: 'uid'
  }
}

The key here is to map the column name in the database.

  • This made it work. Thanks I had to make "id bindable: true" to assing id in constructor, too. – chozero Apr 17 '13 at 10:55
  • True, by default the id excluded in the databinding, that's why you need bindable:true. –  Apr 17 '13 at 11:07
1

In addition to Sergio's answer

The generator strategy is assigned which means application has to assign an id while saving the object. If you have done something like this below then you should be able to get User.get(1) otherwise an exception is thrown while saving without assigning an id

def user = new User(id: 1).save(flush: true)

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • As I commented on Sergio's answer, id wasn't being assigned in constructor, so I had to add: id bindable: true – chozero Apr 17 '13 at 10:56
  • I was doing this but id wasn't being assigned. I had to add id bindable: true to the domain constraints to make this work. – chozero Apr 17 '13 at 10:57
  • I agree. I apologize I actually wanted to mean `user.id = 1` and then `user.save(flush: true)`. Was carried away with the notion of one liners. :) – dmahapatro Apr 17 '13 at 13:29