dat.
The Main Operations
To create the database, we use the following line of code:
ObjectContainer db =
Db4o.openFile("C:\tapestry5\data\celebrities.dat");
If such a file already exists, db4o will just open it, otherwise it will create a new file.
Perhaps the simplest thing we can do is to retrieve all the celebrities stored in the
database. Here is how the getAllCelebrities method of the new data source
will look:
public List
getAllCelebrities()
{
return db.query(Celebrity.class);
}
We just tell the database the representatives of which class we want to retrieve, and it
returns a list, returning all the available objects of that class.
Appendix B
[ 253 ]
If however we want to retrieve a specific object, we'll need to do a little more
work. First of all, we need to select one of the two main approaches to querying a
db4o database??”Query by Example (QBE) or Native Query. QBE is a very simple
approach, but it is limited in a few ways (read the tutorial for details). Anyway, let's
use QBE for the getCelebrityById method. Here is how this method will look:
public Celebrity getCelebrityById(long id)
{
Celebrity proto = new Celebrity();
proto.setId(id);
ObjectSet result = db.
Pages:
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310