Andrey,

This is a correct assessment of the situation: (1) setting persistence state manually is very likely to confuse Cayenne and (2) transient objects can't have relationships. Another way around it is to create NEW objects, and delete them before commit:

MyObject o = context.newObject(MyObject.class);

...

for(Object o : context.newObjects()) {
   if(somecondition) {
      context.deleteObject((Persistent) o);
   }
}

context.commitChanges();

Andrus

On Mar 1, 2008, at 11:13 AM, Razumovsky Andrey wrote:
Hi Adrian,

If i understood your message right, you're manually changing objects' state to COMMITED. Well, i too think this is not a good idea. What would i do in this cause is to make those still non-existing in DB objects using default constructor, so that they would bу TRANSIENT. After that (i think this should be done before calling 'commitChanges', if you don't want to mess with listeners) you should register user-modified objects with Cayenne using 'context.registerNewObject()'. The objects should be registered only if they are TRANSIENT and your-business-logic-modified. Once you do that, their state will change to NEW and they will normally persist. Those objects which are already in DB will normally work since they are COMMITED or MODIFIED. Still, if those objects have relationships you use, this would lead to exceptions during commit, since there is a problem in setting existent objects in relations for transient objects. About this i would write to list in my next question. However, sutuation will be even worse if you have falsely-commited objects, as you suggest.
Hope this helps,
Andrey


AW>
AW> Hello list
AW>
AW> I have some kind of exotic question concerning state of DataObjects. In a AW> scenario I would like to create new DataObjects but keep them in a state AW> so that they only get committed if the user actively changes some default
AW> value.
AW>
AW> Look at it like some lazy persist functionality. Let's say I have a AW> reporting application (which I have not :) ) where I display a matrix to AW> the user. X-axis are the days, y-axis are the projects. In every field in AW> the matrix the user could write the hours he worked on the said project on AW> the specific day. Which means I need to create DataObjects for all fields AW> in the matrix so that the databinding works and the user can enter values AW> into all of the fields. But I only want to persist those records which
AW> actually contain a value.
AW>
AW> I thought I could do so creating new DataObjects, setting default values
AW> and then immediately changing the state of the new DataObjects to
AW> committed. While this generally works (DataObject changes to modified AW> once the user enters a value), the persist process of Cayenne is not AW> amused since it thinks it should update a record in the database which
AW> does not already exist.
AW>
AW> So my question. Would there be another possible solution to my problem?
AW>
AW> Cheers,
AW> Adrian
AW>


Reply via email to