db4o makes your work with persistent objects very simple and straightforward. The only #store(object) method is used for both saving and modification of any object that exists in your model.
01public static void storePilot() { 02
new File(YAPFILENAME).delete(); 03
ObjectContainer db=Db4o.openFile(YAPFILENAME); 04
try { 05
Pilot pilot=new Pilot("Michael Schumacher",0); 06
db.set(pilot); 07
System.out.println("Stored "+pilot); 08
// change pilot and resave updated 09
pilot.addPoints(10); 10
db.set(pilot); 11
System.out.println("Stored "+pilot); 12
} finally { 13
db.close(); 14
} 15
retrieveAllPilots(); 16
}
01public static void updatePilotWrong() { 02
storePilot(); 03
ObjectContainer db=Db4o.openFile(YAPFILENAME); 04
try { 05
// Even completely identical Pilot object 06
// won't work for update of the saved pilot 07
Pilot pilot = new Pilot("Michael Schumacher",10); 08
pilot.addPoints(10); 09
db.set(pilot); 10
System.out.println("Added 10 points for "+pilot); 11
} finally { 12
db.close(); 13
} 14
retrieveAllPilots(); 15
}
01public static void updatePilot() { 02
storePilot(); 03
ObjectContainer db=Db4o.openFile(YAPFILENAME); 04
try { 05
// first retrieve the object from the database 06
ObjectSet result=db.get(new Pilot("Michael Schumacher",10)); 07
Pilot found=(Pilot)result.next(); 08
found.addPoints(10); 09
db.set(found); 10
System.out.println("Added 10 points for "+found); 11
} finally { 12
db.close(); 13
} 14
retrieveAllPilots(); 15
}
Deletion is just as easy:
01public static void deletePilot() { 02
storePilot(); 03
ObjectContainer db=Db4o.openFile(YAPFILENAME); 04
try { 05
// first retrieve the object from the database 06
ObjectSet result=db.get(new Pilot("Michael Schumacher",10)); 07
Pilot found=(Pilot)result.next(); 08
db.delete(found); 09
System.out.println("Deleted "+found); 10
} finally { 11
db.close(); 12
} 13
retrieveAllPilots(); 14
}
The objects are identified by their references in an application cache. You do not need to implement any additional identification systems (like primary keys in RDBMS). See Identity chapter for details. The uniqueness of an object is defined only by its reference, if you will create 2 objects of the same class with exactly the same fields and save them to db4o - you will get 2 objects in your database. As you can see from the examples an object instance should be retrieved from the database before updating or deleting or you can use the newly created object if it was stored in the same session. Creating a new instance identical to the object in the database and saving it, will create a new object in the database.
Db4o does all the "dirty" work of objects transition between your classes and persistent state using Reflection . No mappings or additional coding is needed from your side. If you will need to change your application model for the next version you will also be surprised with the simplicity: all the changes are done in one place - your code, and the most common operations are done completely automatically (see Refactoring And Schema Evolution chapter for details).
Please, remember that all db4o work is done within Transaction, which can be committed or rolled back depending on the result you want to achieve.