Equality Comparison

Db4o uses reference cache for quick access to persistent objects. Each persistent object is guaranteed to have only one instance in the object reference cache independently of whether it was saved or retrieved. You can retrieve the same object several times with different querying methods, but you will still get references to the same object, so that ref(1) == ref(2) == ... == ref(n).

In the same time it means that 2 objects, for example, one created in the runtime and another retrieved from the database, with the same data (field values) won't be equal for db4o.

There are 2 ways to compare db4o objects by data:

Let's save an object to the database and try the above mentioned methods in practice.

EqualityExample.java: storePilot
01private static void storePilot() { 02 ObjectContainer container = database(); 03 if (container != null) { 04 try { 05 Pilot pilot = new Pilot("Kimi Raikkonnen", 100); 06 container.set(pilot); 07 } catch (Exception ex) { 08 System.out.println("System Exception: " + ex.getMessage()); 09 } finally { 10 closeDatabase(); 11 } 12 } 13 }
More Reading: