Objective
In the open source object-oriented database db4o Tour of the 1th part of the article series: in the first db4o, the author introduces the history and present situation of db4o, the Application field, and the comparison with ORM, etc. in the 2nd part: Db4o Query method, the author introduces three different kinds of query methods of db4o: Q Be, SODA, and Native Queries, and each of these three different ways to implement the query of two associated objects.
Before we've covered how to query and add objects in db4o, in this article we'll show you how to update and delete objects in db4o.
Update data
Scene One
Let's imagine a scenario where a man named "John" bought a car, and the license plate (such as the second part of this series code), and his basic information address is not detailed, only wrote "Chengdu City", in a competent department to check the information of the person, found the problem, and immediately proceed to modify.
In db4o, we do this to make changes to this user's information (Listing 1):
Listing 1. Modify Address
package com;
import bo.People;
import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.query.Predicate;
public class DB4OTest{
public static void main(String[] args){
//打开数据库
ObjectContainer db = Db4o.openFile("auto.yap");
try{
ObjectSet<People> result = db.query(new Predicate<People>() {
public boolean match(People people) {
return people.getName().equals("张三");
}
});
People people = result.next();
//修改地址
people.setAddress("成都市金牛区xxx号");
db.set(people);
}finally{
//关闭连接
db.close();
}
}
}
Modify the data is so simple, through NQ query out the People object, then modify its address, and finally save. Now let's see if the modification was successful and open ObjectManager, as shown in Figure 1, we can see that the user data in the database has been updated.
Figure 1. Modify Address
Unlike the second part of this series, we use objectset<people> result to get the return results, not the list<people> List. Consult the Objectset API we found that Objectset actually inherited java.util.List and Java.util.Iterator. Why do you want to inherit two interfaces? This is because db4o intended to be designed for the convenience of developers, and the db4o design goal is lightweight, which provides a variety of features for objectset, without the need for developers to convert between multiple collection interfaces.