You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Brian Baillargeon <bb...@uwo.ca> on 2016/05/19 16:22:25 UTC

Committing changes for specified individual objects

Hey folks,

Generally, I retrieve objects using
BaseContext.getThreadObjectContext().performQuery(query);
and after creating / updating / deleting objects, I commit changes with
BaseContext.getThreadObjectContext().commitChanges();

This commits all changes on the current thread's ObjectContext, but is
there any way to specify which individual objects you want to commit
changes for? I'm just looking for a best practice to safeguard my
objects, but I think this would be a valid use case:
You have a table that logs exceptions, and you encounter an exception
while modifying an object backed by another table. So you want to
persist the exception's log, but not the object you were modifying.

I've thought about separating my objects onto different ObjectContexts
(Ie. children of getThreadObjectContext()), but this doesn't seem like a
good solution, especially if you have relationships (since related
objects have to be in the same ObjectContext).

Any suggestions? I'm using Cayenne 3.0.2, but I'm all ears for 3.1
suggestions too.

Thanks,
Brian Baillargeon


Re: Committing changes for specified individual objects

Posted by Brian Baillargeon <bb...@uwo.ca>.
Thank you, using DataContext.createDataContext() and pulling objects in
with dc.localObject() looks promising! I'll test it out when I get a
chance (getting sidetracked with another project).

Thanks again!

On 2016-05-20 09:58 PM, Michael Gentry wrote:
> Hi Brian,
>
> "is there any way to specify which individual objects you want to commit"
>
> Not really.  Cayenne works at the object graph level and an ObjectContext
> (or DataContext) is what is used to manage your graph changes and then
> commit them in a single database transaction.
>
> That being said, there is nothing stopping you from creating many more
> ObjectContexts and it is cheap to do so.  In your example, you would:
>
> 1) create a brand new ObjectContext totally separate of the thread
> ObjectContext,
> 2) create your exception log object in that new ObjectContext,
> 3) commit the new ObjectContext (which only has your exception log object
> in it).
>
> Since you mentioned you are using 3.0.2, this is easily done with
> DataContext.createDataContext().  Cayenne 3.1 requires you to get it from
> the ServerRuntime.
>
> I'd be wary of creating children of your thread's ObjectContext for this
> purpose as committing child contexts will also commit the parent, if I'm
> remembering correctly.
>
> If your exception log object needs a relationship to an object in the
> thread context, you'll either have to pull it in using DC's localObject()
> method or perhaps DataObjectUtils.objectForPK(), assuming that object is
> already persisted.  I tend to use the latter approach, typically with the
> original object's getObjectId() method.
>
> mrg
>
>
> On Thu, May 19, 2016 at 12:22 PM, Brian Baillargeon <bb...@uwo.ca> wrote:
>
>> Hey folks,
>>
>> Generally, I retrieve objects using
>> BaseContext.getThreadObjectContext().performQuery(query);
>> and after creating / updating / deleting objects, I commit changes with
>> BaseContext.getThreadObjectContext().commitChanges();
>>
>> This commits all changes on the current thread's ObjectContext, but is
>> there any way to specify which individual objects you want to commit
>> changes for? I'm just looking for a best practice to safeguard my
>> objects, but I think this would be a valid use case:
>> You have a table that logs exceptions, and you encounter an exception
>> while modifying an object backed by another table. So you want to
>> persist the exception's log, but not the object you were modifying.
>>
>> I've thought about separating my objects onto different ObjectContexts
>> (Ie. children of getThreadObjectContext()), but this doesn't seem like a
>> good solution, especially if you have relationships (since related
>> objects have to be in the same ObjectContext).
>>
>> Any suggestions? I'm using Cayenne 3.0.2, but I'm all ears for 3.1
>> suggestions too.
>>
>> Thanks,
>> Brian Baillargeon
>>
>>



Re: Committing changes for specified individual objects

Posted by Michael Gentry <bl...@gmail.com>.
Hi Brian,

"is there any way to specify which individual objects you want to commit"

Not really.  Cayenne works at the object graph level and an ObjectContext
(or DataContext) is what is used to manage your graph changes and then
commit them in a single database transaction.

That being said, there is nothing stopping you from creating many more
ObjectContexts and it is cheap to do so.  In your example, you would:

1) create a brand new ObjectContext totally separate of the thread
ObjectContext,
2) create your exception log object in that new ObjectContext,
3) commit the new ObjectContext (which only has your exception log object
in it).

Since you mentioned you are using 3.0.2, this is easily done with
DataContext.createDataContext().  Cayenne 3.1 requires you to get it from
the ServerRuntime.

I'd be wary of creating children of your thread's ObjectContext for this
purpose as committing child contexts will also commit the parent, if I'm
remembering correctly.

If your exception log object needs a relationship to an object in the
thread context, you'll either have to pull it in using DC's localObject()
method or perhaps DataObjectUtils.objectForPK(), assuming that object is
already persisted.  I tend to use the latter approach, typically with the
original object's getObjectId() method.

mrg


On Thu, May 19, 2016 at 12:22 PM, Brian Baillargeon <bb...@uwo.ca> wrote:

> Hey folks,
>
> Generally, I retrieve objects using
> BaseContext.getThreadObjectContext().performQuery(query);
> and after creating / updating / deleting objects, I commit changes with
> BaseContext.getThreadObjectContext().commitChanges();
>
> This commits all changes on the current thread's ObjectContext, but is
> there any way to specify which individual objects you want to commit
> changes for? I'm just looking for a best practice to safeguard my
> objects, but I think this would be a valid use case:
> You have a table that logs exceptions, and you encounter an exception
> while modifying an object backed by another table. So you want to
> persist the exception's log, but not the object you were modifying.
>
> I've thought about separating my objects onto different ObjectContexts
> (Ie. children of getThreadObjectContext()), but this doesn't seem like a
> good solution, especially if you have relationships (since related
> objects have to be in the same ObjectContext).
>
> Any suggestions? I'm using Cayenne 3.0.2, but I'm all ears for 3.1
> suggestions too.
>
> Thanks,
> Brian Baillargeon
>
>

Re: Committing changes for specified individual objects

Posted by Andrus Adamchik <an...@objectstyle.org>.
Hi Brian,

I noticed your mailing list message came through moderation. This means that you won't be getting replies via email (unless you are subscribed with another address). Make sure you check the archives - there are some replies already: https://lists.apache.org/thread.html/Zozc0naffk51vnc

Andrus


> On May 19, 2016, at 7:22 PM, Brian Baillargeon <bb...@uwo.ca> wrote:
> 
> Hey folks,
> 
> Generally, I retrieve objects using
> BaseContext.getThreadObjectContext().performQuery(query);
> and after creating / updating / deleting objects, I commit changes with
> BaseContext.getThreadObjectContext().commitChanges();
> 
> This commits all changes on the current thread's ObjectContext, but is
> there any way to specify which individual objects you want to commit
> changes for? I'm just looking for a best practice to safeguard my
> objects, but I think this would be a valid use case:
> You have a table that logs exceptions, and you encounter an exception
> while modifying an object backed by another table. So you want to
> persist the exception's log, but not the object you were modifying.
> 
> I've thought about separating my objects onto different ObjectContexts
> (Ie. children of getThreadObjectContext()), but this doesn't seem like a
> good solution, especially if you have relationships (since related
> objects have to be in the same ObjectContext).
> 
> Any suggestions? I'm using Cayenne 3.0.2, but I'm all ears for 3.1
> suggestions too.
> 
> Thanks,
> Brian Baillargeon
>