One To Many

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

One To Many

Jonathan Carlson
I hope all my posts are more helpful than annoying.  I'm trying to provide feedback from a stupid user perspective * I guess it comes natural to me--at least when I'm new!  ;-)

1) I noticed that in a one-to-many relationship you can't save the one, and have it save the many.  This is contrary to what the GORM documentation says unless I just did something wacky.  The many objects never get saved unless I save them explicitly.

2) Do you have any hints for bi-direction one-to-many relationships?  I kept getting errors in my ApplicationBootStrap class during test data creation until I made it a one-way relationship (the one holds the many).

Thanks!

- Jonathan


______________________________________________________________________
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

Katun Corporation -- www.katun.com
_____________________________________________________________________
Reply | Threaded
Open this post in threaded view
|

Re: One To Many

graemer
On 3/20/06, Jonathan Carlson <[hidden email]> wrote:
> I hope all my posts are more helpful than annoying.  I'm trying to provide feedback from a stupid user perspective * I guess it comes natural to me--at least when I'm new!  ;-)
>
> 1) I noticed that in a one-to-many relationship you can't save the one, and have it save the many.  This is contrary to what the GORM documentation says unless I just did something wacky.  The many objects never get saved unless I save them explicitly.
Is this for a uni-directional one-to-many?

>
> 2) Do you have any hints for bi-direction one-to-many relationships?  I kept getting errors in my ApplicationBootStrap class during test data creation until I made it a one-way relationship (the one holds the many).
What is the error?

Cheers
Graeme

>
> Thanks!
>
> - Jonathan
>
>
> ______________________________________________________________________
> This email and any files transmitted with it are confidential and
> intended solely for the use of the individual or entity to whom they
> are addressed. If you have received this email in error please notify
> the system manager.
>
> Katun Corporation -- www.katun.com
> _____________________________________________________________________
>
Reply | Threaded
Open this post in threaded view
|

Re: One To Many

graemer
If its a bi-directional relationship where this is occuring remember
that Grails will not manage the relationship between the object model
for you, no ORM solution will do that. What this means is that if you
do:

                def a = new Author(name:'Stephen King')
               
                def b1 = new Book(title:'The Shining', category:'Fiction')
                def b2 = new Book(title:'IT', category:'Fiction')

                a.books.add(b1)
                a.books.add(b1)

Then your books will likely not be saved because you have not
explicity set the author of each book. It is generally recommended
that you write a 'addBook' method, this better encapsulates the
relationship and is better in OO terms:

        def addBook(book) {
                book.author = this
                books.add(book)
        }

Then do:

                a.addBook(b1)
                a.addBook(b2)
               
                a.save()

This will result in the relationships being saved and the integrity of
your object model relationship be retained

I hope this helps

Graeme

On 3/21/06, Graeme Rocher <[hidden email]> wrote:

> On 3/20/06, Jonathan Carlson <[hidden email]> wrote:
> > I hope all my posts are more helpful than annoying.  I'm trying to provide feedback from a stupid user perspective * I guess it comes natural to me--at least when I'm new!  ;-)
> >
> > 1) I noticed that in a one-to-many relationship you can't save the one, and have it save the many.  This is contrary to what the GORM documentation says unless I just did something wacky.  The many objects never get saved unless I save them explicitly.
> Is this for a uni-directional one-to-many?
>
> >
> > 2) Do you have any hints for bi-direction one-to-many relationships?  I kept getting errors in my ApplicationBootStrap class during test data creation until I made it a one-way relationship (the one holds the many).
> What is the error?
>
> Cheers
> Graeme
>
> >
> > Thanks!
> >
> > - Jonathan
> >
> >
> > ______________________________________________________________________
> > This email and any files transmitted with it are confidential and
> > intended solely for the use of the individual or entity to whom they
> > are addressed. If you have received this email in error please notify
> > the system manager.
> >
> > Katun Corporation -- www.katun.com
> > _____________________________________________________________________
> >
>
Reply | Threaded
Open this post in threaded view
|

Re: One To Many

graemer
I've updated the documentation on GORM to reflect the below recommendation:

http://grails.codehaus.org/GORM

Graeme

On 3/21/06, Graeme Rocher <[hidden email]> wrote:

> If its a bi-directional relationship where this is occuring remember
> that Grails will not manage the relationship between the object model
> for you, no ORM solution will do that. What this means is that if you
> do:
>
>                 def a = new Author(name:'Stephen King')
>
>                 def b1 = new Book(title:'The Shining', category:'Fiction')
>                 def b2 = new Book(title:'IT', category:'Fiction')
>
>                 a.books.add(b1)
>                 a.books.add(b1)
>
> Then your books will likely not be saved because you have not
> explicity set the author of each book. It is generally recommended
> that you write a 'addBook' method, this better encapsulates the
> relationship and is better in OO terms:
>
>         def addBook(book) {
>                 book.author = this
>                 books.add(book)
>         }
>
> Then do:
>
>                 a.addBook(b1)
>                 a.addBook(b2)
>
>                 a.save()
>
> This will result in the relationships being saved and the integrity of
> your object model relationship be retained
>
> I hope this helps
>
> Graeme
>
> On 3/21/06, Graeme Rocher <[hidden email]> wrote:
> > On 3/20/06, Jonathan Carlson <[hidden email]> wrote:
> > > I hope all my posts are more helpful than annoying.  I'm trying to provide feedback from a stupid user perspective * I guess it comes natural to me--at least when I'm new!  ;-)
> > >
> > > 1) I noticed that in a one-to-many relationship you can't save the one, and have it save the many.  This is contrary to what the GORM documentation says unless I just did something wacky.  The many objects never get saved unless I save them explicitly.
> > Is this for a uni-directional one-to-many?
> >
> > >
> > > 2) Do you have any hints for bi-direction one-to-many relationships?  I kept getting errors in my ApplicationBootStrap class during test data creation until I made it a one-way relationship (the one holds the many).
> > What is the error?
> >
> > Cheers
> > Graeme
> >
> > >
> > > Thanks!
> > >
> > > - Jonathan
> > >
> > >
> > > ______________________________________________________________________
> > > This email and any files transmitted with it are confidential and
> > > intended solely for the use of the individual or entity to whom they
> > > are addressed. If you have received this email in error please notify
> > > the system manager.
> > >
> > > Katun Corporation -- www.katun.com
> > > _____________________________________________________________________
> > >
> >
>
Reply | Threaded
Open this post in threaded view
|

Re: One To Many

calathus
In reply to this post by graemer
Is it possible to change the set get opertor for books implicitly by grails if the property is declared as  relationship?
All the necessary information seems available for this.

C

On 3/21/06, Graeme Rocher <[hidden email]> wrote:
If its a bi-directional relationship where this is occuring remember
that Grails will not manage the relationship between the object model
for you, no ORM solution will do that. What this means is that if you
do:

                def a = new Author(name:'Stephen King')

                def b1 = new Book(title:'The Shining', category:'Fiction')
                def b2 = new Book(title:'IT', category:'Fiction')

                a.books.add(b1)
                a.books.add(b1)

Then your books will likely not be saved because you have not
explicity set the author of each book. It is generally recommended
that you write a 'addBook' method, this better encapsulates the
relationship and is better in OO terms:

        def addBook(book) {
                book.author = this
                books.add(book)
        }

Then do:

                a.addBook(b1)
                a.addBook(b2)

                a.save()

This will result in the relationships being saved and the integrity of
your object model relationship be retained

I hope this helps

Graeme

On 3/21/06, Graeme Rocher <[hidden email]> wrote:
> On 3/20/06, Jonathan Carlson <[hidden email]> wrote:
> > I hope all my posts are more helpful than annoying.  I'm trying to provide feedback from a stupid user perspective * I guess it comes natural to me--at least when I'm new!  ;-)
> >
> > 1) I noticed that in a one-to-many relationship you can't save the one, and have it save the many.  This is contrary to what the GORM documentation says unless I just did something wacky.  The many objects never get saved unless I save them explicitly.

> Is this for a uni-directional one-to-many?
>
> >
> > 2) Do you have any hints for bi-direction one-to-many relationships?  I kept getting errors in my ApplicationBootStrap class during test data creation until I made it a one-way relationship (the one holds the many).
> What is the error?
>
> Cheers
> Graeme
>
> >
> > Thanks!
> >
> > - Jonathan
> >
> >
> > ______________________________________________________________________
> > This email and any files transmitted with it are confidential and
> > intended solely for the use of the individual or entity to whom they
> > are addressed. If you have received this email in error please notify
> > the system manager.
> >
> > Katun Corporation -- www.katun.com
> > _____________________________________________________________________
> >
>

Reply | Threaded
Open this post in threaded view
|

Re: One To Many

graemer
Sorry calathus I'm not sure I follow, maybe i'm being thick.. can you
elaborate a little?

Thanks
Graeme

On 3/21/06, calathus <[hidden email]> wrote:

> Is it possible to change the set get opertor for books implicitly by grails
> if the property is declared as  relationship?
> All the necessary information seems available for this.
>
>
> C
>
>  On 3/21/06, Graeme Rocher <[hidden email]> wrote:
> > If its a bi-directional relationship where this is occuring remember
> > that Grails will not manage the relationship between the object model
> > for you, no ORM solution will do that. What this means is that if you
> > do:
> >
> >                 def a = new Author(name:'Stephen King')
> >
> >                 def b1 = new Book(title:'The Shining', category:'Fiction')
> >                 def b2 = new Book(title:'IT', category:'Fiction')
> >
> >                 a.books.add(b1)
> >                 a.books.add(b1)
> >
> > Then your books will likely not be saved because you have not
> > explicity set the author of each book. It is generally recommended
> > that you write a 'addBook' method, this better encapsulates the
> > relationship and is better in OO terms:
> >
> >         def addBook(book) {
> >                 book.author = this
> >                 books.add(book)
> >         }
> >
> > Then do:
> >
> >                 a.addBook(b1)
> >                 a.addBook(b2)
> >
> >                 a.save()
> >
> > This will result in the relationships being saved and the integrity of
> > your object model relationship be retained
> >
> > I hope this helps
> >
> > Graeme
> >
> > On 3/21/06, Graeme Rocher <[hidden email]> wrote:
> > > On 3/20/06, Jonathan Carlson <[hidden email] > wrote:
> > > > I hope all my posts are more helpful than annoying.  I'm trying to
> provide feedback from a stupid user perspective * I guess it comes natural
> to me--at least when I'm new!  ;-)
> > > >
> > > > 1) I noticed that in a one-to-many relationship you can't save the
> one, and have it save the many.  This is contrary to what the GORM
> documentation says unless I just did something wacky.  The many objects
> never get saved unless I save them explicitly.
> > > Is this for a uni-directional one-to-many?
> > >
> > > >
> > > > 2) Do you have any hints for bi-direction one-to-many relationships?
> I kept getting errors in my ApplicationBootStrap class during test data
> creation until I made it a one-way relationship (the one holds the many).
> > > What is the error?
> > >
> > > Cheers
> > > Graeme
> > >
> > > >
> > > > Thanks!
> > > >
> > > > - Jonathan
> > > >
> > > >
> > > >
> ______________________________________________________________________
> > > > This email and any files transmitted with it are confidential and
> > > > intended solely for the use of the individual or entity to whom they
> > > > are addressed. If you have received this email in error please notify
> > > > the system manager.
> > > >
> > > > Katun Corporation -- www.katun.com
> > > >
> _____________________________________________________________________
> > > >
> > >
> >
>
>
Reply | Threaded
Open this post in threaded view
|

Re: One To Many

calathus
Graeme,
Since I just started using/evaluating Groovy/Grails, I don't know how much extent they support AOP like approach, but at least it contains asm, cglib library which support these style, so I wondered it can use some method interception technique or if @Property is specially handled to generate setter/getter, It  may change the code generation based on provided meta info of relationship in another property.
Even if it is possible, it would require some caution to avoid infinite recursion of add invoation if m:m relation is used..

C

On 3/21/06, Graeme Rocher <[hidden email]> wrote:
Sorry calathus I'm not sure I follow, maybe i'm being thick.. can you
elaborate a little?

Thanks
Graeme

On 3/21/06, calathus <[hidden email]> wrote:
> Is it possible to change the set get opertor for books implicitly by grails

> if the property is declared as  relationship?
> All the necessary information seems available for this.
>
>
> C
>
>  On 3/21/06, Graeme Rocher <[hidden email]> wrote:
> > If its a bi-directional relationship where this is occuring remember
> > that Grails will not manage the relationship between the object model
> > for you, no ORM solution will do that. What this means is that if you
> > do:
> >
> >                 def a = new Author(name:'Stephen King')
> >
> >                 def b1 = new Book(title:'The Shining', category:'Fiction')
> >                 def b2 = new Book(title:'IT', category:'Fiction')
> >
> >                 a.books.add(b1)
> >                 a.books.add(b1)
> >
> > Then your books will likely not be saved because you have not
> > explicity set the author of each book. It is generally recommended
> > that you write a 'addBook' method, this better encapsulates the
> > relationship and is better in OO terms:
> >
> >         def addBook(book) {
> >                 book.author = this
> >                 books.add(book)
> >         }
> >
> > Then do:
> >
> >                 a.addBook(b1)
> >                 a.addBook(b2)
> >
> >                 a.save()
> >
> > This will result in the relationships being saved and the integrity of
> > your object model relationship be retained
> >
> > I hope this helps
> >
> > Graeme
> >
> > On 3/21/06, Graeme Rocher <[hidden email]> wrote:
> > > On 3/20/06, Jonathan Carlson < [hidden email] > wrote:
> > > > I hope all my posts are more helpful than annoying.  I'm trying to
> provide feedback from a stupid user perspective * I guess it comes natural
> to me--at least when I'm new!  ;-)
> > > >
> > > > 1) I noticed that in a one-to-many relationship you can't save the
> one, and have it save the many.  This is contrary to what the GORM
> documentation says unless I just did something wacky.  The many objects
> never get saved unless I save them explicitly.
> > > Is this for a uni-directional one-to-many?
> > >
> > > >
> > > > 2) Do you have any hints for bi-direction one-to-many relationships?
> I kept getting errors in my ApplicationBootStrap class during test data
> creation until I made it a one-way relationship (the one holds the many).
> > > What is the error?
> > >
> > > Cheers
> > > Graeme
> > >
> > > >
> > > > Thanks!
> > > >
> > > > - Jonathan
> > > >
> > > >
> > > >
> ______________________________________________________________________
> > > > This email and any files transmitted with it are confidential and
> > > > intended solely for the use of the individual or entity to whom they
> > > > are addressed. If you have received this email in error please notify
> > > > the system manager.
> > > >
> > > > Katun Corporation -- www.katun.com
> > > >
> _____________________________________________________________________
> > > >
> > >
> >
>
>

Reply | Threaded
Open this post in threaded view
|

Re: One To Many

Jonathan Carlson
In reply to this post by Jonathan Carlson
Thanks for your suggestions, Graeme, about creating an add method on the
"one" side.  I did that and somehow bidirectional seems to work now,
although I must have been doing something else wrong.

The error I was getting was a hibernate error...  something about an ID
not being set.... like I was trying to add a relationship with an object
that didn't have an ID yet.  

I'm trying to make much fewer changes now before testing things out so
I know exactly what's different.  I'm still in the Java habit of being
able to change lots of stuff before testing.  That's one benefit of
straight Java - You get to use an IDE that flags syntax errors
immediately.

Thanks,

Jonathan



>>> [hidden email] 2006-03-20 5:18:04 PM >>>
I hope all my posts are more helpful than annoying.  I'm trying to
provide feedback from a stupid user perspective * I guess it comes
natural to me--at least when I'm new!  ;-)

1) I noticed that in a one-to-many relationship you can't save the one,
and have it save the many.  This is contrary to what the GORM
documentation says unless I just did something wacky.  The many objects
never get saved unless I save them explicitly.

2) Do you have any hints for bi-direction one-to-many relationships?  I
kept getting errors in my ApplicationBootStrap class during test data
creation until I made it a one-way relationship (the one holds the
many).

Thanks!

- Jonathan


______________________________________________________________________
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

Katun Corporation -- www.katun.com
_____________________________________________________________________

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________

______________________________________________________________________
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

Katun Corporation -- www.katun.com
_____________________________________________________________________
Reply | Threaded
Open this post in threaded view
|

Re: One To Many

graemer
In reply to this post by calathus
On 3/21/06, calathus <[hidden email]> wrote:
> Graeme,
> Since I just started using/evaluating Groovy/Grails, I don't know how much
> extent they support AOP like approach, but at least it contains asm, cglib
> library which support these style, so I wondered it can use some method
> interception technique or if @Property is specially handled to generate
> setter/getter, It  may change the code generation based on provided meta
> info of relationship in another property.
@Property does generate a getter/setter yes, and groovy allows you to
access getters/setters as if they were public fields.

Graeme

> Even if it is possible, it would require some caution to avoid infinite
> recursion of add invoation if m:m relation is used..
>
>
> C
>
> On 3/21/06, Graeme Rocher <[hidden email]> wrote:
> > Sorry calathus I'm not sure I follow, maybe i'm being thick.. can you
> > elaborate a little?
> >
> > Thanks
> > Graeme
> >
> > On 3/21/06, calathus <[hidden email]> wrote:
> > > Is it possible to change the set get opertor for books implicitly by
> grails
> > > if the property is declared as  relationship?
> > > All the necessary information seems available for this.
> > >
> > >
> > > C
> > >
> > >  On 3/21/06, Graeme Rocher <[hidden email]> wrote:
> > > > If its a bi-directional relationship where this is occuring remember
> > > > that Grails will not manage the relationship between the object model
> > > > for you, no ORM solution will do that. What this means is that if you
> > > > do:
> > > >
> > > >                 def a = new Author(name:'Stephen King')
> > > >
> > > >                 def b1 = new Book(title:'The Shining',
> category:'Fiction')
> > > >                 def b2 = new Book(title:'IT', category:'Fiction')
> > > >
> > > >                 a.books.add(b1)
> > > >                 a.books.add(b1)
> > > >
> > > > Then your books will likely not be saved because you have not
> > > > explicity set the author of each book. It is generally recommended
> > > > that you write a 'addBook' method, this better encapsulates the
> > > > relationship and is better in OO terms:
> > > >
> > > >         def addBook(book) {
> > > >                 book.author = this
> > > >                 books.add(book)
> > > >         }
> > > >
> > > > Then do:
> > > >
> > > >                 a.addBook(b1)
> > > >                 a.addBook(b2)
> > > >
> > > >                 a.save()
> > > >
> > > > This will result in the relationships being saved and the integrity of
> > > > your object model relationship be retained
> > > >
> > > > I hope this helps
> > > >
> > > > Graeme
> > > >
> > > > On 3/21/06, Graeme Rocher <[hidden email]> wrote:
> > > > > On 3/20/06, Jonathan Carlson < [hidden email] > wrote:
> > > > > > I hope all my posts are more helpful than annoying.  I'm trying to
> > > provide feedback from a stupid user perspective * I guess it comes
> natural
> > > to me--at least when I'm new!  ;-)
> > > > > >
> > > > > > 1) I noticed that in a one-to-many relationship you can't save the
> > > one, and have it save the many.  This is contrary to what the GORM
> > > documentation says unless I just did something wacky.  The many objects
> > > never get saved unless I save them explicitly.
> > > > > Is this for a uni-directional one-to-many?
> > > > >
> > > > > >
> > > > > > 2) Do you have any hints for bi-direction one-to-many
> relationships?
> > > I kept getting errors in my ApplicationBootStrap class during test data
> > > creation until I made it a one-way relationship (the one holds the
> many).
> > > > > What is the error?
> > > > >
> > > > > Cheers
> > > > > Graeme
> > > > >
> > > > > >
> > > > > > Thanks!
> > > > > >
> > > > > > - Jonathan
> > > > > >
> > > > > >
> > > > > >
> > >
> ______________________________________________________________________
> > > > > > This email and any files transmitted with it are confidential and
> > > > > > intended solely for the use of the individual or entity to whom
> they
> > > > > > are addressed. If you have received this email in error please
> notify
> > > > > > the system manager.
> > > > > >
> > > > > > Katun Corporation -- www.katun.com
> > > > > >
> > >
> _____________________________________________________________________
> > > > > >
> > > > >
> > > >
> > >
> > >
> >
>
>
Reply | Threaded
Open this post in threaded view
|

Re: One To Many

graemer
In reply to this post by Jonathan Carlson
On 3/21/06, Jonathan Carlson <[hidden email]> wrote:
> Thanks for your suggestions, Graeme, about creating an add method on the
> "one" side.  I did that and somehow bidirectional seems to work now,
> although I must have been doing something else wrong.
>
> The error I was getting was a hibernate error...  something about an ID
> not being set.... like I was trying to add a relationship with an object
> that didn't have an ID yet.
Yes, this error is because the child has not had its parent set hence
the parent id is missing hence the exception.. by doing my suggestion
you are doing this. Hibernate or GORM doesn't do this for you (ie
manage your relational model). This is still something you have to do
yourself

>
> I'm trying to make much fewer changes now before testing things out so
> I know exactly what's different.  I'm still in the Java habit of being
> able to change lots of stuff before testing.  That's one benefit of
> straight Java - You get to use an IDE that flags syntax errors
> immediately.
Indeed, thats why its nice that grails supports a blended approach :-)

Graeme

>
> Thanks,
>
> Jonathan
>
>
>
> >>> [hidden email] 2006-03-20 5:18:04 PM >>>
> I hope all my posts are more helpful than annoying.  I'm trying to
> provide feedback from a stupid user perspective * I guess it comes
> natural to me--at least when I'm new!  ;-)
>
> 1) I noticed that in a one-to-many relationship you can't save the one,
> and have it save the many.  This is contrary to what the GORM
> documentation says unless I just did something wacky.  The many objects
> never get saved unless I save them explicitly.
>
> 2) Do you have any hints for bi-direction one-to-many relationships?  I
> kept getting errors in my ApplicationBootStrap class during test data
> creation until I made it a one-way relationship (the one holds the
> many).
>
> Thanks!
>
> - Jonathan
>
>
> ______________________________________________________________________
> This email and any files transmitted with it are confidential and
> intended solely for the use of the individual or entity to whom they
> are addressed. If you have received this email in error please notify
> the system manager.
>
> Katun Corporation -- www.katun.com
> _____________________________________________________________________
>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>
> ______________________________________________________________________
> This email and any files transmitted with it are confidential and
> intended solely for the use of the individual or entity to whom they
> are addressed. If you have received this email in error please notify
> the system manager.
>
> Katun Corporation -- www.katun.com
> _____________________________________________________________________
>