You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by emeka okafor <em...@yahoo.com> on 2013/01/05 14:12:46 UTC

testing many to many

 I stumbled across a weird thing today and believe that it has something to do with the fact that I am a cayenne newbie. Here is the situation:

I have a many-to-many relationship: Blog <<=>> DataSource. 
I have configured it in the modeler the simplest way possible, without any specific relationship table and no flattened relation.
I have this test snippet: 

System.out.println(blog1.getDatasources().get(0));                                    // prints datasource0
System.out.println(blog1.getDatasources().get(1));                                    // prints datasouce1

blog1.getObjectContext().deleteObjects(blog1.getDatasources().get(1));  // delete datasource1
blog1.getObjectContext().commitChanges();                                            // commit changes
System.out.println(blog1.getDatasources().get(0));                                     // prints datasource0
System.out.println(blog1.getDatasources().get(1));                                     // prints datasource1 <= WHY?
Assert.assertEquals(1, blog1.getDatasources().size());                                // FAILS
 
Am I doing something wrong here?

Thanks.

Re: testing many to many

Posted by Michael Gentry <mg...@masslight.net>.
Hi there,

You told Cayenne to delete the DataSource (datasource1) from the database,
but you didn't remove datasource1 from the object graph.  Cayenne usually
considers these separate operations*.  This is why you can still call:

System.out.println(blog1.getDatasources().get(0)); // prints datasource0
System.out.println(blog1.getDatasources().get(1)); // prints datasource1 <=
WHY?

Your datasource1 object is still in the object graph even though you
deleted it from the database.  Try adding something like
blog1.removeFromDatasources(blog1.getDatasources().get(1)) after your
deleteObjects call.

*The reason why Cayenne keeps remove/delete separate is because you may
want to remove a a DataSource from one Blog and attach it to another Blog.
 Also, sometimes you just want to unlink the Datasource, but not delete it.
 Therefore, you tell Cayenne explicitly to remove from the object graph and
to delete the object.

mrg

PS. I *think* you can put a Nullify delete rule from DataSource to Blog to
have it auto-remove (datasource1) from the object graph, but it has been
ages since I've tried that, so I most likely am wrong.  I personally prefer
to have both operations (remove and delete) explicitly stated in the code
so I can see exactly the intent there instead of having to look at two
different sources (code+model).



On Sat, Jan 5, 2013 at 8:12 AM, emeka okafor <em...@yahoo.com> wrote:

>  I stumbled across a weird thing today and believe that it has something
> to do with the fact that I am a cayenne newbie. Here is the situation:
>
> I have a many-to-many relationship: Blog <<=>> DataSource.
> I have configured it in the modeler the simplest way possible, without any
> specific relationship table and no flattened relation.
> I have this test snippet:
>
> System.out.println(blog1.getDatasources().get(0));
>            // prints datasource0
> System.out.println(blog1.getDatasources().get(1));
>            // prints datasouce1
>
> blog1.getObjectContext().deleteObjects(blog1.getDatasources().get(1));  //
> delete datasource1
> blog1.getObjectContext().commitChanges();
>            // commit changes
> System.out.println(blog1.getDatasources().get(0));
>             // prints datasource0
> System.out.println(blog1.getDatasources().get(1));
>             // prints datasource1 <= WHY?
> Assert.assertEquals(1, blog1.getDatasources().size());
>            // FAILS
>
> Am I doing something wrong here?
>
> Thanks.