Technically, ObjectId is the way to check that two objects across contexts are 
pointing to the same DB row. Whether this constitutes equality from a Java 
perspective or not is a very situational. Sometimes it is, other times it is 
not (e.g., when you are comparing property values). 

So while I don't recall at the moment how (un)safe it is to override 
Object.equals(..) from the framework standpoint (probably ok), I usually avoid 
doing that for the philosophical reason above :) The alternatives are either 
specific comparison methods like "idEquals(..)", using Map<ObjectId, ?> instead 
of a Set, etc.

Andrus

> On May 15, 2026, at 8:51 AM, Robert A. Decker <[email protected]> wrote:
> 
> How should I be checking equality between persistent objects, to use a 
> java.util.HashSet for example?
> 
> Is the ObjectId of a BaseDataObject the same across all ObjectContexts for a 
> database row?
> 
> Do people using cayenne override the equals method:
>  return this.getObjectId().equals(other.getObjectId());
> 
> Or is there a different way people usually check equality? (compare primary 
> keys, etc?)
> 
> Rob

Reply via email to