Hi Ryan,

Ryan Vanderwerf wrote:
I'm using 0.9.7 one of our developers is having a strange cache problem:
when I get an object, change its primary key to 0,
Please tell your colleage that he does not really want to change primary key attributes ;-). This is no good idea in applications backed by a relational database!

store it (it then
creates a new row with a new primary key in the database), then retrieve
the original object, it returns the wrong one.
setting a PK to null or 0 triggers the sequence manager to compute a valid ID !

For example,  I get object ID 1. I then set its primary key to 0, store
it, thus creating a new object. Now I try to get object ID 1 again, and
it returns instead the new record I just created, even though it doesn't
even match the primary key I  just used.
The cache maps OIDs to Objects. under OID 1 it still holds the reference to the original object. manipulating PKs does not manipulate java references !

Calling broker.clearCache()
makes it works, but calling broker.removeFromCache(app) doesn't do
anything.
calling removeFromCache with the *original* (before setting it to 0) Identity should do the trick!

 Is there a better solution that flushing the whole cache to
make this situation work? Another thing of note, is that the SQL
generated on the getById() is the correct SQL - it's just the cache is
confused and returning the wrong object.

Mhh. I don't see any error in the cache implementation here.
IMO its an application problem.
Why do you want to change the primary key of an object?
This is generally bad practise in RDBMS apps.
If you do so, you are responsible to maintain data integrity!

cheers,
Thomas


Application app =
AppManager.getById(form.getApplication_id());
int parAppID = app.getApplication_id();
app.setApplication_id(0);

broker.store(app);

broker.clearCache();

Applications application = getById(parAppID); // without
the clearCache, it returns the object I just stored, which has a
different pri key


public static Applications getById(int appId) {


Applications app = null;

Criteria criteria = new Criteria();

criteria.addEqualTo(Applications.APPLICATION_ID, new
Integer(appId));

Query query = new QueryByCriteria(Applications.class, criteria);

try {

PersistenceBroker broker = getBroker();

app = (Applications) broker.getObjectByQuery(query);

} catch (Exception e) {

Log.error(Applications.class, "Error getById()", e);

}


return app;

}


// xml object (no foreign keys or anything funky)

<class-descriptor

class="com.xxx.xxx2.beans.Applications"

table="XXX_APPLICATIONS"

>
<field-descriptor id="1"

name="application_id"

column="APPLICATION_ID"

jdbc-type="INTEGER"

primarykey="true"

autoincrement="true"

/>

<field-descriptor id="2"

name="application_name"

column="APPLICATION_NAME"

jdbc-type="VARCHAR"

/>

<field-descriptor id="3"

name="application_description"

column="APPLICATION_DESCRIPTION"

jdbc-type="VARCHAR"

/>

<field-descriptor id="4"

name="version"

column="VERSION"

jdbc-type="VARCHAR"

/>

<field-descriptor id="5"

name="registration_date"

column="REGISTRATION_DATE"

jdbc-type="TIMESTAMP"

/>

<field-descriptor id="6"

name="catalog_flag"

column="CATALOG_FLAG"

jdbc-type="VARCHAR"

/>

<field-descriptor id="7"

name="catalog_entry_date"

column="CATALOG_ENTRY_DATE"

jdbc-type="TIMESTAMP"

/>

<field-descriptor id="8"

name="pending_flag"

column="PENDING_FLAG"

jdbc-type="VARCHAR"

/>

<field-descriptor id="9"

name="certification_flag"

column="CERTIFICATION_FLAG"

jdbc-type="VARCHAR"

/>

<field-descriptor id="10"

name="certification_date"

column="CERTIFICATION_DATE"

jdbc-type="TIMESTAMP"

/>

<field-descriptor id="11"

name="rejected_reason"

column="REJECTED_REASON"

jdbc-type="VARCHAR"

/>

<field-descriptor id="12"

name="launch_date"

column="LAUNCH_DATE"

jdbc-type="TIMESTAMP"

/>

<field-descriptor id="13"

name="application_status_change_date"

column="APPLICATION_STATUS_CHANGE_DATE"

jdbc-type="TIMESTAMP"

/>

<field-descriptor id="14"

name="revenue_generated"

column="REVENUE_GENERATED"

jdbc-type="VARCHAR"

/>

<field-descriptor id="15"

name="pricing"

column="PRICING"

jdbc-type="VARCHAR"

/>

<field-descriptor id="16"

name="user_id"

column="USER_ID"

jdbc-type="INTEGER"

/>

<field-descriptor id="17"

name="created_by"

column="CREATED_BY"

jdbc-type="VARCHAR"

/>

<field-descriptor id="18"

name="created_date"

column="CREATED_DATE"

jdbc-type="TIMESTAMP"

/>

<field-descriptor id="19"

name="updated_by"

column="UPDATED_BY"

jdbc-type="VARCHAR"

/>

<field-descriptor id="20"

name="certification_type_id"

column="CERTIFICATION_TYPE_ID"

jdbc-type="INTEGER"

/>

<field-descriptor id="21"

name="status_id"

column="STATUS_ID"

jdbc-type="INTEGER"

/>

<field-descriptor id="22"

name="updated_date"

column="UPDATED_DATE"

jdbc-type="TIMESTAMP"

/>

<field-descriptor id="23"

name="pending_modification_id"

column="PENDING_MODIFICATION_ID"

jdbc-type="INTEGER"

/>

</class-descriptor>





--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to