« Return to Thread: saving a one-to-many

Re: saving a one-to-many

by graemer :: Rate this Message:

Reply to Author | View in Thread

Hi Corey,

You appear to be doing the best you can :-)

You don't need to call save() multiple times however. We may one day
add a feature that means you don't have to new objects in order to
create relationships

Something like:

def serviceOrder = new ServiceOrder(
  contact    : params["contact"] ,
  creditCard : params["creditCard]
)
serviceOrder.addOrderLine(
     serviceOrder : serviceOrder,
     product      : Product.get( params.productId )
)
serviceOrder.save()

But atm there is not much more you can do, hence the lack of response :-)

Cheers


On 3/13/07, Corey <corey_s@...> wrote:

>
> Warnock applies?  (c8=
>
> http://en.wikipedia.org/wiki/Warnock's_Dilemma
>
> On Sunday 11 March 2007 06:19:34 pm Corey wrote:
> > First off, thanks for bearing with me here - I do appreciate all help and
> > advice.
> <snip>
> > To summarize:
> >
> > I'm looking for the most idiomatic and/or succinct/direct method of
> > saving/persisting a one-to-many domain class instance.
> >
> > I have something called a ServiceOrder, which can contain many line items,
> > facilitated via an OrderLine. Each OrderLine associates one or more
> > Products with any particular ServiceOrder:
> >
> > .../domain/ServiceOrder.groovy:
> >
> > class ServiceOrder {
> >
> >    static hasMany = [ items : OrderLine ]
> >
> >    Set        items      = new HashSet()
> >
> >    Contact    contact
> >    CreditCard creditCard
> >
> > }
> >
> > .../domain/OrderLine.groovy:
> >
> > class OrderLine {
> >
> >    static belongsTo = ServiceOrder
> >
> >    Integer      quantity = 1
> >
> >    ServiceOrder serviceOrder
> >    Product      product
> >
> > }
> >
> >
> > Now, what would be the generally recommended way to create and save a
> > ServiceOrder, complete with one or more OrderLines?
> >
> > My original attempt was:
> >
> > def serviceOrder = new ServiceOrder(
> >    contact    : new Contact( params ),
> >    creditCard : new CreditCard( params )
> > )
> > serviceOrder.save()
> >
> > serviceOrder.addOrderLine(
> >    new OrderLine(
> >       serviceOrder : serviceOrder,
> >       product      : Product.get( params.productId )
> >    )
> > )
> > serviceOrder.save()
> >
> >
> > ... which works, but - I don't know - I just get the suspicion this
> > approach is somehow naive; is my hunch wrong? Or is there actually a
> > "better" way?
> >
> > I was then helped along with Fred's advice to declare items as a
> > Set/HashSet in my ServiceOrder domain class, which led me to the following
> > attempt:
> >
> > def serviceOrder = new ServiceOrder(
> >    contact    : new Contact( params ),
> >    creditCard : new CreditCard( params ),
> >    items      : new OrderLine( product : Product.get( params.productId ) )
> > )
> > serviceOrder.save()
> >
> >
> > ... which is more along the lines of what I'm looking for, however there
> > appears to be an issue with this because doing so in this manner creates an
> > OrderLine with a null serviceOrder:
> >
> > dev=# select * from order_line ;
> >  id | version | product_id | service_order_id | quantity
> > ----+---------+------------+------------------+----------
> >  30 |       0 |         18 |                  |        1
> >
> >
> > It's this general sense of a chicken-and-egg situation, in addition to my
> > being quite new to grails/gorm/groovy, which is prompting me to seek advice
> > from the list. Obviously, if the first approach works, then it works - but
> > I'm interested in how someone more experienced might achieve the same
> > results, or how I should modify the second approach to work correctly.
> >
> > Many thanks!
> >
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
Graeme Rocher
Grails Project Lead
http://grails.org

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

    http://xircles.codehaus.org/manage_email

 « Return to Thread: saving a one-to-many