Explicitly assign id to domain class?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Explicitly assign id to domain class?

John Moore
I know that the id property of a domain class is a special case and is auto-assigned unless you configure it otherwise. I've run into a situation where I would ideally like to override this occasionally, assigning an id to an existing domain object, and I was wondering whether there was some way round this.

At certain stages in the life of a Policy in my system, I store a JSON representation of the entire instance, so that at some point in the future I can go back and see what the precise state of the Policy object was at that stage. But when I go to 'hydrate' this JSON data to a Policy instance, I note that the id is in fact always null, even though the id is there in the actual JSON data. This is the code:

def slurper = new JsonSlurper()
Policy policy = slurper.parseText(policyData) 

So although all the other properties of the object have been successfully restored, the id has not. Is there some way to get around this?
Reply | Threaded
Open this post in threaded view
|

Re: Explicitly assign id to domain class?

sergiomichels
You can set the generator to your id field. http://grails.org/doc/latest/ref/Database%20Mapping/id.html

static mapping = {
  id generator: 'assigned' //manually assigned before saving the instance
}


--
Sérgio Michels


On Wed, Oct 30, 2013 at 5:53 PM, John Moore <[hidden email]> wrote:
I know that the id property of a domain class is a special case and is
auto-assigned unless you configure it otherwise. I've run into a situation
where I would ideally like to override this occasionally, assigning an id to
an existing domain object, and I was wondering whether there was some way
round this.

At certain stages in the life of a Policy in my system, I store a JSON
representation of the entire instance, so that at some point in the future I
can go back and see what the precise state of the Policy object was at that
stage. But when I go to 'hydrate' this JSON data to a Policy instance, I
note that the id is in fact always null, even though the id is there in the
actual JSON data. This is the code:



So although all the other properties of the object have been successfully
restored, the id has not. Is there some way to get around this?



--
View this message in context: http://grails.1312388.n4.nabble.com/Explicitly-assign-id-to-domain-class-tp4651017.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Explicitly assign id to domain class?

John Moore
sergiomichels wrote
You can set the generator to your id field.
http://grails.org/doc/latest/ref/Database%20Mapping/id.html

static mapping = {
  id generator: 'assigned' //manually assigned before saving the instance
}
Sorry, I should have made it clearer - that was what I was referring to by 'unless you configure it otherwise'. I wanted to stick with the standard auto-assigning, but be able to override if necessary. I guess this isn't possible. It may well be that the 'assigned' option is the only path I can go down with this.
Reply | Threaded
Open this post in threaded view
|

Re: Explicitly assign id to domain class?

bksaville
We investigated doing the same thing (basically we needed an import type functionality which sounds very similar to what you are trying to do), and didn't find a better way of doing it.  I believe you have to make it assigned.  However, you may be able to use a beforeInsert method on the domain class to assign the ID if it is not already specified.  We weren't able to just because we are using the mongo plugin instead of hibernate.

-Brian


On Wed, Oct 30, 2013 at 2:51 PM, John Moore <[hidden email]> wrote:
sergiomichels wrote
> You can set the generator to your id field.
> http://grails.org/doc/latest/ref/Database%20Mapping/id.html
>
> static mapping = {
>   id generator: 'assigned' //manually assigned before saving the instance
> }

Sorry, I should have made it clearer - that was what I was referring to by
'unless you configure it otherwise'. I wanted to stick with the standard
auto-assigning, but be able to override if necessary. I guess this isn't
possible. It may well be that the 'assigned' option is the only path I can
go down with this.



--
View this message in context: http://grails.1312388.n4.nabble.com/Explicitly-assign-id-to-domain-class-tp4651017p4651021.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Explicitly assign id to domain class?

burtbeckwith
In reply to this post by John Moore
There should be a way to do this with a custom hybrid IdentifierGenerator implementation that uses the id that's set if there is one, but auto-assigns one otherwise. I don't know how you'd do this with an auto-increment column (perhaps do what org.hibernate.id.SelectGenerator does) or use a sequence. For this to work you'd probably need to make the id bindable - http://grails.org/doc/latest/ref/Constraints/bindable.html

Burt

John Moore wrote
sergiomichels wrote
You can set the generator to your id field.
http://grails.org/doc/latest/ref/Database%20Mapping/id.html

static mapping = {
  id generator: 'assigned' //manually assigned before saving the instance
}
Sorry, I should have made it clearer - that was what I was referring to by 'unless you configure it otherwise'. I wanted to stick with the standard auto-assigning, but be able to override if necessary. I guess this isn't possible. It may well be that the 'assigned' option is the only path I can go down with this.