This one time, at band camp, Jeff Klann said:

JK>Ok, another JDO newbie question...
JK>
JK>I don't understand the JDO persistence model. I have a method called
JK>save(MyClass obj), and I want it check to see if a record with obj.key
JK>(it's identity) exists in the database. If it does, I want it to update
JK>that record by replacing it with the new record, otherwise I want it to
JK>create a new record.
JK>
JK>First, when doing an OQL query to discover whether the object is already in
JK>the database, it seems I have to load the entire record again (which could
JK>be quite large). Is there any way around that? (I'm currently using "select
JK>p from blah.MyClass p where key = $1".)

This is a very common situation. Here is some pseudo code representing
how to handle the situation. Yes, the object needs to be loaded.
Unless it is fully loaded into the cache, Castor has no idea that
any field is dirty.

    db.begin();
    fooOql = db.getOQLQuery( "select p from blah.MyClass p where key = $1" );
    fooOql.bind( bar.getId() );
    results = fooOql.execute();
    while ( results.hasMore() )
    {
        foo = ( Foo )results.next();
        foo.setXXX();
        foo.setYYY();
    db.commit();

JK>Second, why can't I update(obj)? Or remove(obj)? It has the proper identity
JK>set, but I can apparently only update and remove objects that have actually
JK>been loaded from the database. So, right now, I'm writing:
JK>remove(results.next()); create(obj); ... which seems like a hackish way of
JK>doing things. What am I missing?

Yes, this is correct. Again, Castor's cache is unaware of the object
until it is fully loaded. Also, please be aware that db.update()
is *only* for use with long transactions:

    http://castor.exolab.org/long-transact.html

Bruce
--

perl -e 'print unpack("u30","<0G)U8V4\@4VYY9&5R\"F9E<G)E=\$\!F<FEI+F-O;0\`\`");'

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to