go4it

just do it

DB4O学习(三)对象的查询

1.三种查询方式

  (1)QBE--只能精确查找

  (2)Simple Object Data Access(SODA)--DB4O的一套查询API

  (3)Native Queries(NQ)--调整查询以达到更高的查询速度

 

2.三种查询方式的对比

image

 

3.QBE的几种方式:

(1)通过构造器指定参数

Person template = new Person();
template.setName("Ben");
ObjectSet res = db.get(template);
 

(2)通过setter指定参数

Person template = new Person();
template.setName("Ben");
template.setAge(42);
ObjectSet res = db.get(template);
 

(3)其他方法

Person template = new Person(null, 82);   // match age only Person template = new Person("Ben", 0);   // match name only
Person template = new Person(null, 0);    // ensure an empty template

 

(4)指定类型

ObjectSet res = db.get(Person.class);   // JAVA
 

(5)获取全部类型

ObjectSet res = db.get(null);   // JAVA

 

4.QBE的几个问题

• You can’t use the values that are defined to mean “empty” as constraints. Fields specified
as null, 0, or "" (empty string) are treated as unconstrained, which could be a problem
in some cases. For example, what if you actually want to find Person objects with _age
equal to 0? You can’t when using QBE.

如果想查找age为0的person则不能用QBE.


• You might run into problems if your classes initialize fields when they are declared. For
example, what if the Person class initializes the attribute string _name = "Joe"? In that
case, a query that uses the default constructor to create an “empty” template will only
return “Joe” objects. To get a truly empty template, you would have to explicitly set the
_name attribute to null.

对于类内部初始化的情况要特别注意初始化的值。

posted on 2009-02-19 16:30  cxccbv  阅读(413)  评论(0)    收藏  举报

导航