RE: Flushing new/dirty Objects before Query

From: Fehlhammer, Manuel (manuel.fehlhamme..dm.de)
Date: Fri Nov 21 2003 - 11:04:20 EST

  • Next message: Andrus Adamchik: "Re: Flushing new/dirty Objects before Query"

    -----Ursprüngliche Nachricht-----
    Von: Andrus Adamchik [mailto:andru..bjectstyle.org]
    Gesendet: Donnerstag, 20. November 2003 23:25
    An: cayenne-use..bjectstyle.org
    Betreff: Re: Flushing new/dirty Objects before Query
    Hi Andrus

    >Looks like you have a case with a meaningful PK included in the java
    >class. Am I right?
    >
    >This is the only case that won't be handled properly right now. If PK
    >is not the part of the class and is generated, there simply won't be a
    >case when you can fetch a record that is the same as a new uncommitted
    >object.
    >
    >If my assertion is true, lets find a workaround.
    >
    >1. First, do you really need to make a PK column a writable object
    >property? For read only access to object id values you can do something
    >like object.getObjectId().getValueForAttribute("XYZ"). There are lots
    >of valid cases when this is unavoidable, just checking that yours is
    >one of those.
    >
    >2. Assuming that you do need to have it. Then lets at least make a
    >smaller time window for this problem. Before creating a new object and
    >assigning it a value, do a fetch to see if such object is already in
    >the database. Use fetched object if it exists, create a new one only
    >if it does not. This approach makes sense to me from the general object
    >identity idea (outside of Cayenne), and I hope it works for you.
    >
    >In the meantime, let me think if we can or should handle this
    >transparently in Cayenne. This will require rewriting ObjectId values
    >on the fly, and arbitrary changing ObjectId contents can have negative
    >consequences.

    Ok - I changed my code - my Objects now have NO meaningfull PK anymore - I let Cayenne generate the pk/oid. However my problem still exists - new Object is found by query only after commit:

    code snip:

       ctxt= DataContext.create();
       Customer newCustomer = new Customer();
       newCustomer.setId(123);
       ctxt.registerNewObject(newCustomer);

       // now query for this new Customer (id is unique - note id is NOT primaryKey)
       Expression objPathExp = ExpressionFactory.binaryPathExp(Expression.EQUAL_TO,"id", new Long(id));
       SelectQuery query = new SelectQuery(Customer.class);
       query.setQualifier(objPathExp);

       // At this point resultList is empty!!!!
       List resultList = ctxt.performQuery(query);

       ctxt.commitChanges()

       // here the new Customer is in the resultList
       resultList = ctxt.performQuery(query);
    end code

    By the way - I'm using Cayenne 1.02 - looked at the code and didn't find any place, where this scenario is handled.

    Ciao,

    Manuel

       



    This archive was generated by hypermail 2.0.0 : Fri Nov 21 2003 - 11:04:31 EST