|
View:
New views
9 Messages
—
Rating Filter:
Alert me
|
|
|
saving a one-to-manyI feel like I'm doing this in more of an idiotic, rather than idiomatic fashion. I have something called a ServiceOrder, which can contain many line items, for which I've created an OrderLine. Each OrderLine associates one or more Products with any particular ServiceOrder. I'm wondering how to save a ServiceOrder ( one-to-many ) in the most succinct way possible. Do I save the ServiceOrder first, then simply begin adding OrderLines, as I do below - or is there a more direct/elegant/fashionable way of doing this? .../domain/ServiceOrder.groovy: class ServiceOrder { static hasMany = [ items : OrderLine ] static constraints = { contact( nullable : false ) creditCard( nullable : false ) } Contact contact CreditCard creditCard } .../domain/OrderLine.groovy: class OrderLine { static belongsTo = ServiceOrder /* are the following constraints desired/valid? static constraints = { serviceOrder( nullable : false ) product( nullable : false ) } */ Integer quantity = 1 ServiceOrder serviceOrder Product product } .../controllers/ServiceOrder.groovy: // am I doing this "correctly"? private save = { def serviceOrder = new ServiceOrder( contact : new Contact( params ), creditCard : new CreditCard( params ) ) serviceOrder.save() // there should be a loop here, but I'm simplifying for // sake of brevity serviceOrder.addOrderLine( new OrderLine( serviceOrder : serviceOrder, product : Product.get( params.productId ) ) ) serviceOrder.save() [ serviceOrder : serviceOrder ] } --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: saving a one-to-manyI wonder why your controller is named ServiceOrder.groovy and not ServiceOrderController.groovy? I think the requirement is that 'Controller' has to be part of the name, unless that has been dropped in 0.4.x. Did you use the "generate all" or "generate controller" commands?
Then
class ServiceOrder {
static hasMany = [ items : OrderLine ] ===> missing Set items = new HashSet()
Then in the controller just creating and adding an OrderLine to items will trigger the automatic save of both by Hibernate after the controller action retuns
Fred
On 3/10/07, Corey <corey_s@...> wrote:
|
|
|
Re: saving a one-to-manyOn Saturday 10 March 2007 03:46:03 pm Fred Janon wrote:
> I wonder why your controller is named ServiceOrder.groovy and not > ServiceOrderController.groovy? > My bad! Was a typo, ServiceOrderController.groovy is correct. > > Then > class ServiceOrder { > > static hasMany = [ items : OrderLine ] > > ===> missing > Set items = new HashSet() > > Then in the controller just creating and adding an OrderLine to items will > trigger the automatic save of both by Hibernate after the controller action > retuns > That's perfect! In fact I saw that referenced somewhat cryptically in the docs ( http://grails.codehaus.org/GORM+-+Defining+relationships ), but was unable to figure out exactly how it was relevant: " If you want, you can define the property like this: "Set books = new HashSet()" " Thanks so much for responding, very much appreciated! Beers, Corey > Fred > > On 3/10/07, Corey <corey_s@...> wrote: > > I feel like I'm doing this in more of an idiotic, rather than idiomatic > > fashion. > > > > I have something called a ServiceOrder, which can contain many line > > items, for which I've created an OrderLine. Each OrderLine associates one > > or more Products with any particular ServiceOrder. > > > > I'm wondering how to save a ServiceOrder ( one-to-many ) in the most > > succinct > > way possible. Do I save the ServiceOrder first, then simply begin adding > > OrderLines, as I do below - or is there a more direct/elegant/fashionable > > way > > of doing this? > > > > > > .../domain/ServiceOrder.groovy: > > > > ServiceOrder.groovy > > static constraints = { > > contact( nullable : false ) > > creditCard( nullable : false ) > > } > > > > Contact contact > > CreditCard creditCard > > > > } > > > > .../domain/OrderLine.groovy: > > > > class OrderLine { > > > > static belongsTo = ServiceOrder > > > > /* are the following constraints desired/valid? > > static constraints = { > > serviceOrder( nullable : false ) > > product( nullable : false ) > > } > > */ > > > > Integer quantity = 1 > > > > ServiceOrder serviceOrder > > Product product > > > > } > > > > > > .../controllers/ServiceOrder.groovy: > > > > // am I doing this "correctly"? > > private save = { > > > > def serviceOrder = new ServiceOrder( > > contact : new Contact( params ), > > creditCard : new CreditCard( params ) > > ) > > serviceOrder.save() > > > > // there should be a loop here, but I'm simplifying for > > // sake of brevity > > serviceOrder.addOrderLine( > > new OrderLine( > > serviceOrder : serviceOrder, > > product : Product.get( params.productId ) > > ) > > ) > > serviceOrder.save() > > > > [ serviceOrder : serviceOrder ] > > > > } > > > > > > > > > > --------------------------------------------------------------------- > > To unsubscribe from this list please visit: > > > > http://xircles.codehaus.org/manage_email --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
|
groovy.org.esHi all, just wanted to let you know that we have just launched groovy.org.es, a portal about groovy and grails for the spanish speaking community. We hope to contribute to "spread the word" among such a big and active community of Java developers. Hope you like it! Nacho Brito Calahorro +34 677 421 557 ImaginaWorks Software Factory, S.L. www.imaginaworks.com |
|
|
RE: groovy.org.esLooks
very nice! Congratulations!
Dierk
|
|
|
Re: saving a one-to-manyFirst off, thanks for bearing with me here - I do appreciate all help and advice. Ok, onward: > On Saturday 10 March 2007 03:46:03 pm Fred Janon wrote: <snip> > > Set items = new HashSet() > > > > Then in the controller just creating and adding an OrderLine to items > > will trigger the automatic save of both by Hibernate after the controller > > action retuns > After toying with this, I discovered that it (seemingly) still doesn't quite do what I'm after. 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 |
|
|
Re: saving a one-to-manyWarnock 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 |
|
|
Re: saving a one-to-manyHi 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 |
|
|
Re: saving a one-to-manyOn Tuesday 13 March 2007 02:46:03 am Graeme Rocher wrote:
> Hi Corey, > > You appear to be doing the best you can :-) > > You don't need to call save() multiple times however. > Cool - that's just the sort of tidbit I was hoping to glean; I had been saving multiple times in similar instances as well, thinking/assuming that the id of the owning object needed to be generated via the save before I could properly attach the relationship. ( or something like that... (c8= ) > 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] > ) > Interesting, that would make for a convenient shortcut. > But atm there is not much more you can do, hence the lack of response :-) > Heheh - understood - and thanks for your time in responding! Cheers, Corey --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
| Free embeddable forum powered by Nabble | Forum Help |