Quantcast

How To Avoid Loading Associated Objects When I Know Their ID

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How To Avoid Loading Associated Objects When I Know Their ID

bdrhoa
There are cases where I already know the the id of an associated object. For example this works:

 def tt = new Tagtype(customer:Customer.get(1),substitutiongroup:'dc:title',tagtype:'Title',regexpression:Regexpression.get(1)).save(flush:true)

but how can I do something like:

 def tt = new Tagtype(customer:1,substitutiongroup:'dc:title',tagtype:'Title',regexpression:1).save(flush:true)

to avoid loading the other objects when I don't really need them?

Thanks!
---------------------------
www.maf.org/rhoads
www.ontherhoads.org
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How To Avoid Loading Associated Objects When I Know Their ID

Daniel Henrique Alves Lima
    I'm not sure if this is the best way, but sometimes i use
"non-updatable relationships" for one-to-one relationships where the ids
are in a known set of possibilities. I can't remember the proper
notation, but:

    class Tagtype {

          static mapping {
              customerId column:'ID_CUSTOMER'
              customer column:'ID_CUSTOMER', insertable: false,
updateable: false          
          }

           long customerId  // updatable
           Customer customer  // read-only
   }

    Then i use "fetch join" to load the relationship. But i use this
kind of mapping for very particular cases.

Brad Rhoads wrote:
> There are cases where I already know the the id of an associated
> object. For example this works:
> (...)

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How To Avoid Loading Associated Objects When I Know Their ID

Dana
In reply to this post by bdrhoa
Try using load(1) instead of get(1). I haven't done sql logging to confirm that it avoids a DB call, but the documentation[1] suggests that it'll save you a trip to the database if all you're using is the id of the object.

[1] http://grails.org/doc/latest/ref/Domain%20Classes/load.html

On 3/9/11 2:49 PM, Brad Rhoads wrote:
There are cases where I already know the the id of an associated object. For example this works:

 def tt = new Tagtype(customer:Customer.get(1),substitutiongroup:'dc:title',tagtype:'Title',regexpression:Regexpression.get(1)).save(flush:true)

but how can I do something like:

 def tt = new Tagtype(customer:1,substitutiongroup:'dc:title',tagtype:'Title',regexpression:1).save(flush:true)

to avoid loading the other objects when I don't really need them?

Thanks!
---------------------------
www.maf.org/rhoads
www.ontherhoads.org
Loading...