One of the ways to do that is using evaluation API. Evaluation/candidate classes are serialized and sent to the server. That means that if we will put the selection criteria and update code inside evaluation class, we will have that code on the server and executing a query using evaluation on the client side will run the update code on the server side.
For easier query execution we can use database singleton - a class that have only one instance saved in the database. That actually can be the class calling the query itself.
Let's fill up our server database:
01private static void setObjects() { 02
new File(DB4O_FILE_NAME).delete(); 03
ObjectContainer container = Db4o.openFile(DB4O_FILE_NAME); 04
try { 05
for (int i = 0; i < 5; i++) { 06
Car car = new Car("car" + i); 07
container.set(car); 08
} 09
container.set(new RemoteExample()); 10
} finally { 11
container.close(); 12
} 13
checkCars(); 14
}
Now we can update the cars using specially designed singleton:
01private static void updateCars() { 02
// triggering mass updates with a singleton 03
// complete server-side execution 04
Configuration configuration = Db4o.newConfiguration(); 05
configuration.messageLevel(0); 06
ObjectServer server = Db4o.openServer(configuration, DB4O_FILE_NAME, 0); 07
try { 08
ObjectContainer client = server.openClient(); 09
Query q = client.query(); 10
q.constrain(RemoteExample.class); 11
q.constrain(new Evaluation() { 12
public void evaluate(Candidate candidate) { 13
// evaluate method is executed on the server 14
// use it to run update code 15
ObjectContainer objectContainer = candidate 16
.objectContainer(); 17
Query q2 = objectContainer.query(); 18
q2.constrain(Car.class); 19
ObjectSet objectSet = q2.execute(); 20
while (objectSet.hasNext()) { 21
Car car = (Car) objectSet.next(); 22
car.setModel("Update1-" + car.getModel()); 23
objectContainer.set(car); 24
} 25
objectContainer.commit(); 26
} 27
}); 28
q.execute(); 29
client.close(); 30
} finally { 31
server.close(); 32
} 33
checkCars(); 34
}
This method has its pros and cons.
Pros:
Cons: