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:
01private static void Test1() 02
{ 03
IObjectContainer container = Database(); 04
if (container != null) 05
{ 06
try 07
{ 08
// Pilot1 contains re-initialisation in the constructor 09
Pilot1 pilot = new Pilot1("Kimi Raikonnen", 100); 10
container.Set(pilot); 11
container.Commit(); 12
// QBE returns result with wrong points 13
IObjectSet result = container.Get(new Pilot1("Kimi Raikonnen", 100)); 14
System.Console.WriteLine("Test QBE on class with member re-initialization in constructor"); 15
ListResult(result); 16
} 17
catch (Db4oException ex) 18
{ 19
System.Console.WriteLine("Db4o Exception: " + ex.Message); 20
} 21
catch (Exception ex) 22
{ 23
System.Console.WriteLine("System Exception: " + ex.Message); 24
} 25
finally 26
{ 27
CloseDatabase(); 28
} 29
} 30
}
01Private Shared Sub Test1() 02
Dim container As IObjectContainer = Database() 03
If container IsNot Nothing Then 04
Try 05
' Pilot1 contains re-initialisation in the constructor 06
Dim pilot As New Pilot1("Kimi Raikonnen", 100) 07
container.[Set](pilot) 08
container.Commit() 09
' QBE returns result with wrong points 10
Dim result As IObjectSet = container.[Get](New Pilot1("Kimi Raikonnen", 100)) 11
System.Console.WriteLine("Test QBE on class with member re-initialization in constructor") 12
ListResult(result) 13
Catch ex As Db4oException 14
System.Console.WriteLine("Db4o Exception: " + ex.Message) 15
Catch ex As Exception 16
System.Console.WriteLine("System Exception: " + ex.Message) 17
Finally 18
CloseDatabase() 19
End Try 20
End If 21
End Sub
2. This example is similar to the previous one, but uses a class derived from the class in test1:
01private static void Test2() 02
{ 03
IObjectContainer container = Database(); 04
if (container != null) 05
{ 06
try 07
{ 08
// Pilot1Derived derives the constructor with re-initialisation 09
Pilot1Derived pilot = new Pilot1Derived("Kimi Raikonnen", 100); 10
container.Set(pilot); 11
container.Commit(); 12
// QBE returns result with wrong points 13
IObjectSet result = container.Get(new Pilot1Derived("Kimi Raikonnen", 100)); 14
System.Console.WriteLine("Test QBE on class with member re-initialization in ancestor constructor"); 15
ListResult(result); 16
} 17
catch (Db4oException ex) 18
{ 19
System.Console.WriteLine("Db4o Exception: " + ex.Message); 20
} 21
catch (Exception ex) 22
{ 23
System.Console.WriteLine("System Exception: " + ex.Message); 24
} 25
finally 26
{ 27
CloseDatabase(); 28
} 29
} 30
}
01Private Shared Sub Test2() 02
Dim container As IObjectContainer = Database() 03
If container IsNot Nothing Then 04
Try 05
' Pilot1Derived derives the constructor with re-initialisation 06
Dim pilot As New Pilot1Derived("Kimi Raikonnen", 100) 07
container.[Set](pilot) 08
container.Commit() 09
' QBE returns result with wrong points 10
Dim result As IObjectSet = container.[Get](New Pilot1Derived("Kimi Raikonnen", 100)) 11
System.Console.WriteLine("Test QBE on class with member re-initialization in ancestor constructor") 12
ListResult(result) 13
Catch ex As Db4oException 14
System.Console.WriteLine("Db4o Exception: " + ex.Message) 15
Catch ex As Exception 16
System.Console.WriteLine("System Exception: " + ex.Message) 17
Finally 18
CloseDatabase() 19
End Try 20
End If 21
End Sub
3. This example uses QBE against a class with static member initialization:
01private static void Test3() 02
{ 03
IObjectContainer container = Database(); 04
if (container != null) 05
{ 06
try 07
{ 08
// Pilot2 uses static value to initializ points 09
Pilot2 pilot = new Pilot2("Kimi Raikonnen", 100); 10
container.Set(pilot); 11
container.Commit(); 12
// QBE returns result with wrong points 13
IObjectSet result = container.Get(new Pilot2("Kimi Raikonnen", 100)); 14
System.Console.WriteLine("Test QBE on class with member initialization using static value"); 15
ListResult(result); 16
} 17
catch (Db4oException ex) 18
{ 19
System.Console.WriteLine("Db4o Exception: " + ex.Message); 20
} 21
catch (Exception ex) 22
{ 23
System.Console.WriteLine("System Exception: " + ex.Message); 24
} 25
finally 26
{ 27
CloseDatabase(); 28
} 29
} 30
}
01Private Shared Sub Test3() 02
Dim container As IObjectContainer = Database() 03
If container IsNot Nothing Then 04
Try 05
' Pilot2 uses static value to initializ points 06
Dim pilot As New Pilot2("Kimi Raikonnen", 100) 07
container.[Set](pilot) 08
container.Commit() 09
' QBE returns result with wrong points 10
Dim result As IObjectSet = container.[Get](New Pilot2("Kimi Raikonnen", 100)) 11
System.Console.WriteLine("Test QBE on class with member initialization using static value") 12
ListResult(result) 13
Catch ex As Db4oException 14
System.Console.WriteLine("Db4o Exception: " + ex.Message) 15
Catch ex As Exception 16
System.Console.WriteLine("System Exception: " + ex.Message) 17
Finally 18
CloseDatabase() 19
End Try 20
End If 21
End Sub
4. This example is similar to test3, but a derived class is used:
01private static void Test4() 02
{ 03
IObjectContainer container = Database(); 04
if (container != null) 05
{ 06
try 07
{ 08
// Pilot2Derived is derived from class with initialization of points member using static value 09
Pilot2Derived pilot = new Pilot2Derived("Kimi Raikonnen", 100); 10
container.Set(pilot); 11
container.Commit(); 12
// QBE returns result with wrong points 13
IObjectSet result = container.Get(new Pilot2Derived("Kimi Raikonnen", 100)); 14
System.Console.WriteLine("Test QBE on class derived from a class with member initialization using static member"); 15
ListResult(result); 16
} 17
catch (Db4oException ex) 18
{ 19
System.Console.WriteLine("Db4o Exception: " + ex.Message); 20
} 21
catch (Exception ex) 22
{ 23
System.Console.WriteLine("System Exception: " + ex.Message); 24
} 25
finally 26
{ 27
CloseDatabase(); 28
} 29
} 30
}
01Private Shared Sub Test4() 02
Dim container As IObjectContainer = Database() 03
If container IsNot Nothing Then 04
Try 05
' Pilot2Derived is derived from class with initialization of points member using static value 06
Dim pilot As New Pilot2Derived("Kimi Raikonnen", 100) 07
container.[Set](pilot) 08
container.Commit() 09
' QBE returns result with wrong points 10
Dim result As IObjectSet = container.[Get](New Pilot2Derived("Kimi Raikonnen", 100)) 11
System.Console.WriteLine("Test QBE on class derived from a class with member initialization using static member") 12
ListResult(result) 13
Catch ex As Db4oException 14
System.Console.WriteLine("Db4o Exception: " + ex.Message) 15
Catch ex As Exception 16
System.Console.WriteLine("System Exception: " + ex.Message) 17
Finally 18
CloseDatabase() 19
End Try 20
End If 21
End Sub