Using Equals

In some cases you can't use QBE as a retrieval method. In these cases you must override the object's equals method to allow you to compare objects data. For example:

Pilot.java: equals
1public boolean equals(Pilot p){ 2 return name.equals(p.getName()) && points == p.getPoints(); 3 }

Note, that you must implement hashCode/GetHashCode method, when you implement equals:

Pilot.java: hashCode
1public int hashCode(){ 2 return name.hashCode() ^ points; 3 }

Now we can use the equals method to compare an object from the database to an object prototype:

EqualityExample.java: testEquality
01private static void testEquality() { 02 ObjectContainer container = database(); 03 if (container != null) { 04 try { 05 ObjectSet<Pilot> result = container.query(new Predicate<Pilot>(){ 06 public boolean match(Pilot pilot){ 07 return pilot.getName().equals("Kimi Raikkonnen") && 08 pilot.getPoints() == 100; 09 } 10 }); 11 Pilot obj = (Pilot)result.next(); 12 Pilot pilot = new Pilot("Kimi Raikkonnen", 100); 13 String equality = obj.equals(pilot) ? "equal" : "not equal"; 14 System.out.println("Pilots are " + equality); 15 } catch (Exception ex) { 16 System.out.println("System Exception: " + ex.getMessage()); 17 } finally { 18 closeDatabase(); 19 } 20 } 21 }