We had something similar here where a batch job needed to read a data file
and insert new records or update existing records (handle duplicates,
essentially).  What we did was:

* Read the data file, find the key data.
* Write and call a getFoo(dataContext, keys) method.
* The getFoo() method would query the database and if it found a record, it
would return the Cayenne object to us.  If no record was found, it would
create and register a new object in the DC and return the new object.
* Then we'd apply all the data in the data file to the object return by
getFoo().
* Then dataContext.commitChanges().
* Repeat until we processed all the data.

Seemed to work pretty well and I think that approach would work for you,
too, most likely.  In your case, your POJO is the equivalent of a record in
our data file.  This also frees you up from having to care about the
persistence state.  You don't care.  You have an object, apply the changes,
commit.

/dev/mrg

PS. If relationships are involved, your getFoo() should create all the
objects for all of the required relationships if you have to create/register
new objects (when there was no DB match).  This way your main processing
code can just apply changes blindly without having to check if the
relationships exist.


On 6/21/07, Michael Lepine <[EMAIL PROTECTED]> wrote:

I've got a situation where I've got strict POJO objects that I'll need to
copy data from and into my generated Cayenne classes. My issue is that
when
I copy the data from the bean to the Cayenne class, I don't know whether
the
object exists or not. Thus, I create the Cayenne class instance using
DataContext.newObject(). Obviously, when I call DataContext.commitChanges
(),
an insert is being attempted on the corresponding table even if a record
already exists in the database.

Is there a way to create the Cayenne instance so that the persistent layer
will know to check whether the record exists and update it instead of
always
attempting an insert?

Any help and guidance are appreciated.

Reply via email to