cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Musall, Maik" <>
Subject Re: CommitChangesToParent() <> CommitChanges()
Date Thu, 23 Feb 2017 08:18:10 GMT
Hi Markus,

> I'm having some troubles understanding commitChanges() and
> commitChangesToParent() :-/
> I've created a test case with two ObjectContexts
> ObjectContext context1 = runtime.newContext();
> ObjectContext context2 = runtime.newContext();

Those two don't have a parent/child relationship. For that you would have to create context2
= runtime.newContext( context1 );

> then I load the same entity in both contexts
> PpAfkoPordershead order1 = PpAfkoPordershead.getByAufnr(context1, 123);
> PpAfkoPordershead order2 = PpAfkoPordershead.getByAufnr(context2, 123);
> then I tried this
> Integer quantity = order1.getQuantity();
> order1.setQuantity(quantity + 1);
> context1.commitChanges();
> Assert.assertEquals(quantity, order2.getQuantity());
> this works as the second context get synched by the commit.

Btw, this synchronization is on by default, and can be switched off. Personally I can see
the default changing some time in the future, because in multithreaded applications this can
lead to difficult runtime problems when data that one thread is operating on changes mid-operation.
For more details, see this chapter in the Cayenne Guide:

> but why does this work too?
> Integer quantity = order1.getQuantity();
> order1.setQuantity(quantity + 1);
> context1.commitChangesToParent();
> Assert.assertEquals(quantity, order2.getQuantity());

Works the same because the parent is the same, see answer part #1. But that isn't what parent/child
contexts are about anyway. The concept is more like if context2 is a child of context1, you
could commit something in context2 to parent, and that would become visible in context1 as
well, but wouldn't hit the database until you also commit in context1.

I'm fairly new to Cayenne as well, so if I got something wrong, I'm sure someone will correct
me. :-)


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message