Then we created
another object with exactly the same data and saved it too. For db4o, as this database
operates with objects, any two distinct objects are different, so it will just save the
second Bill Clinton object without any doubt, and we shall have two objects with the
same data, which is probably not what we really want.
To avoid such a problem, we need to check whether an object with exactly the same
values already exists before saving an object to the database. So here is a more robust
implementation of the addCelebrity method:
public void addCelebrity(Celebrity c)
{
ObjectSet result = db.get(c);
if (!result.hasNext())
{
db.set(c);
}
}
I cannot finish this demonstration of db4o without showing you an example of a
Native Query??”the approach we are most often going to use in real life applications.
This approach isn't much more complex than QBE, by the way, but more efficient
and powerful.
Let us, just for the purposes of this appendix, implement the getRange(int
indexFrom, int indexTo) method in a way slightly different from how it was
dealt with in the main body of the book. Say we want this method to return a list
of Celebrity objects whose ID property is greater than or equal to the indexFrom
parameter and less than or equal to the indexTo parameter.
Pages:
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312