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 <[email protected]> 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 > >
