QBE Limitations

As it was mentioned before QBE has some serious limitations.

Query-By-Example evaluates all non-null fields and all simple type variables that do not hold their default values against the stored objects. Check to make sure that you are not constraining the resultset by accidentally initializing variables on your template objects. Typical places could be:

The following classes provide an example of classes that cannot be used with QBE:

The following examples show the results of QBE usage with the classes above. Note, that there are some differences between Java and .NET behavior:

1. QBE used against a class that has arbitrary member initialization in the constructor: 

QBEExample.java: test1
01private static void test1() { 02 ObjectContainer container = database(); 03 if (container != null) { 04 try { 05 // Pilot1 contains initialisation in the constructor 06 Pilot1 pilot = new Pilot1("Kimi Raikkonen"); 07 container.set(pilot); 08 // QBE does not return any results 09 ObjectSet result = container.get(new Pilot1("Kimi Raikonnen")); 10 System.out.println("Test QBE on class with member initialization in constructor"); 11 listResult(result); 12 } catch (Exception ex) { 13 System.out.println("System Exception: " + ex.getMessage()); 14 } finally { 15 closeDatabase(); 16 } 17 } 18 }
 

2. This example is similar to the previous one, but uses a class derived from the class in test1:

QBEExample.java: test2
01private static void test2() { 02 ObjectContainer container = database(); 03 if (container != null) { 04 try { 05 // Pilot1Derived derives the constructor with initialisation 06 Pilot1Derived pilot = new Pilot1Derived("Kimi Raikkonen"); 07 container.set(pilot); 08 // QBE does not return any results 09 ObjectSet result = container.get(new Pilot1Derived("Kimi Raikonnen")); 10 System.out.println("Test QBE on class with member initialization in ancestor constructor"); 11 listResult(result); 12 } catch (Exception ex) { 13 System.out.println("System Exception: " + ex.getMessage()); 14 } finally { 15 closeDatabase(); 16 } 17 } 18 }
 

3. This example uses QBE against a class with static member initialization:

QBEExample.java: test3
01private static void test3() { 02 ObjectContainer container = database(); 03 if (container != null) { 04 try { 05 // Pilot2 uses static initialization of points member 06 Pilot2 pilot = new Pilot2("Kimi Raikkonen"); 07 container.set(pilot); 08 // QBE does not return any results 09 ObjectSet result = container.get(new Pilot2("Kimi Raikonnen")); 10 System.out.println("Test QBE on class with static member initialization"); 11 listResult(result); 12 } catch (Exception ex) { 13 System.out.println("System Exception: " + ex.getMessage()); 14 } finally { 15 closeDatabase(); 16 } 17 } 18 }
 

4. This example is similar to test3, but a derived class is used:

QBEExample.java: test4
01private static void test4() { 02 ObjectContainer container = database(); 03 if (container != null) { 04 try { 05 // Pilot2Derived is derived from class with static initialization of points member 06 Pilot2Derived pilot = new Pilot2Derived("Kimi Raikkonen"); 07 container.set(pilot); 08 // QBE does not return any results 09 ObjectSet result = container.get(new Pilot2Derived("Kimi Raikonnen")); 10 System.out.println("Test QBE on class derived from a class with static member initialization"); 11 listResult(result); 12 } catch (Exception ex) { 13 System.out.println("System Exception: " + ex.getMessage()); 14 } finally { 15 closeDatabase(); 16 } 17 } 18 }