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

Re: saving a one-to-many

by Bugzilla from corey_s@qwest.net :: Rate this Message:

Reply to Author | View in Thread


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

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