For the following examples we will use DataObject and ListObject classes. The database will be filled up with the FillUpDb method.
If you want to delete all members in a list you can use remove list function.
01private static void removeTest() { 02
ObjectContainer db = Db4o.openFile(DBFILE); 03
try { 04
// set update depth to 1 as we only 05
// modify List field 06
db.ext().configure().objectClass(ListObject.class).updateDepth(1); 07
List<ListObject> result = db.<ListObject> query(ListObject.class); 08
if (result.size() > 0) { 09
// retrieve a ListObject 10
ListObject lo1 = result.get(0); 11
// remove all the objects from the list 12
lo1.getData().removeAll(lo1.getData()); 13
db.set(lo1); 14
} 15
} finally { 16
db.close(); 17
} 18
// check DataObjects in the list 19
// and DataObjects in the database 20
db = Db4o.openFile(DBFILE); 21
try { 22
List<ListObject> result = db.<ListObject> query(ListObject.class); 23
if (result.size() > 0) { 24
ListObject lo1 = result.get(0); 25
System.out.println("DataObjects in the list: " 26
+ lo1.getData().size()); 27
} 28
List<DataObject> removedObjects = db 29
.<DataObject> query(DataObject.class); 30
System.out.println("DataObjects in the database: " 31
+ removedObjects.size()); 32
} finally { 33
db.close(); 34
} 35
}
However as you will see from the example above, removed objects are not deleted from the database. Here you should be very careful: if you want to delete DataObjects, which were removed from the list you must be sure that they are not referenced by existing objects. Check
Referential Integrity article for more information.
The following example shows how to delete DataObjects from the database as well as from the list:
01private static void removeAndDeleteTest() { 02
ObjectContainer db = Db4o.openFile(DBFILE); 03
try { 04
// set update depth to 1 as we only 05
// modify List field 06
db.ext().configure().objectClass(ListObject.class).updateDepth(1); 07
List<ListObject> result = db.<ListObject> query(ListObject.class); 08
if (result.size() > 0) { 09
// retrieve a ListObject 10
ListObject lo1 = result.get(0); 11
// create a copy of the objects list 12
// to memorize the objects to be deleted 13
List tempList = new ArrayList(lo1.getData()); 14
// remove all the objects from the list 15
lo1.getData().removeAll(lo1.getData()); 16
// and delete them from the database 17
Iterator<DataObject> it = tempList.iterator(); 18
while (it.hasNext()) { 19
db.delete(it.next()); 20
} 21
22
db.set(lo1); 23
} 24
} finally { 25
db.close(); 26
} 27
// check DataObjects in the list 28
// and DataObjects in the database 29
db = Db4o.openFile(DBFILE); 30
try { 31
List<ListObject> result = db.<ListObject> query(ListObject.class); 32
if (result.size() > 0) { 33
ListObject lo1 = result.get(0); 34
System.out.println("DataObjects in the list: " 35
+ lo1.getData().size()); 36
} 37
List<DataObject> removedObjects = db 38
.<DataObject> query(DataObject.class); 39
System.out.println("DataObjects in the database: " 40
+ removedObjects.size()); 41
} finally { 42
db.close(); 43
} 44
}