Binding objects

Db4o adds additional flexibility to its reference system allowing the user to re-associate an object with its stored instance or to replace an object in database:

Java: ExtObjectContainer#bind(object,id)

Typical usecases could be:

The following requirements should be met:

  • The ID needs to be a valid internal object ID, previously retrieved with ExtObjectContainer#getID(object)
  • The object parameter needs to be of the same class as the stored object.

Calling ExtObjectContainer#bind(object,id) does not have any impact on persisted objects. It only attaches the new object to the database identity. ObjectContainer#set(object) should be used to persist the change.

Let's look how it works in practice.

IdentityExample.java: testBind
01public static void testBind(){ 02 setObjects(); 03 ObjectContainer db = Db4o.openFile(Util.YAPFILENAME); 04 try { 05 Query q = db.query(); 06 q.constrain(Car.class); 07 q.descend("model").constrain("Ferrari"); 08 ObjectSet result = q.execute(); 09 Car car1 = (Car)result.get(0); 10 long IdCar1 = db.ext().getID(car1); 11 Car car2 = new Car("BMW", new Pilot("Rubens Barrichello")); 12 db.ext().bind(car2,IdCar1); 13 db.set(car2); 14 15 result = db.query(Car.class); 16 listResult(result); 17 } finally { 18 db.close(); 19 } 20 }

So this method gives you control over internal object storage. But its usage is potentially dangerous and normally should be avoided. Use ExtObjectContainer#bind(object,id) only for short-lived objects and in controlled situations where no other references exist.