db-jdo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <e...@jpox.org>
Subject invoke List.contains with elements in memory vs in database
Date Sat, 24 Dec 2005 09:38:47 GMT
I got a user question on the behavior of contains, but not sure whether
the current JPOX behavior is correct. 

####################
I recently ran into a problem where calling org.jpox.sco.List.contains()
gave me different results under (what I thought were) the same runtime
conditions. After stepping through the code, I now understand why I am
getting different results. I illustrate the issue below.

Basically, I have a collection (List) of persistent objects. At some
point in my code, I want to determine if that collection already
contains a particular object based on equality (Object.equals()), so I
invoke contains():

    if (myList.contains(anObject)) {


The interesting bit here is that anObject is not persistent. In fact,
it's not even enhanced. It is a POJO that happens to implement the same
interface used by the persistent objects stored in the collection.

Assume that there is indeed an object in the collection for which
objectInCollection.equals(anObject) is true. Thus, I would expect
List.contains() to return true, as well.

In the case where the above code does return true, I see that useCache
and isCacheLoaded are both true, so the delegate is used to implement
the method. Since this delegate is simply an ArrayList, I get the
behavior I expect.

When isCacheLoaded is not true, the method delegates to backingStore. As
part of the backingStore.contains() call, the method
org.jpox.store.rdbms.scostore.AbstractCollectionStore.validateElementFor
Reading() method is called (moved to ElementContainerStore in HEAD).
This method returns false if the element is not persistent AND not
detached. Since the parameter I originally passed is neither persistent
nor detached, contains() always returns false in this state.

Am I just asking for trouble by passing an object not of the PC type to
the contains() method of a persistent collection?

I worked around the issue by explicitly iterating over the elements of
the collection and invoking equals() on each one until I find a match.
This feels like a hack to me because I know in three months someone else
will come along and make the same mistake because they are thinking in
terms of java.util.List.contains(), not JDO.
####################

Thanks,

Erik Bengtson 



Mime
View raw message