Introduction
This lab introduces the use of object databases in the context of web applications. We shall use db4o (http://db4o.com), a lightweight object database. This enables complex objects (i.e. object graphs) to be saved very easily. Care must be taken, however, to choose the most appropriate form of object model. While saving an object is very simple, retrieving and updating the object can be more complex.
In this lab we shall work through some code to create, save, retrieve and modify some objects. Then we'll do this within the context of a web application.
Again, it is strongly recommended that you use Intellij for this. You can add the files you create during this lab to an existing project.
We'll begin by working through the code examples in the lecture notes. The code is repeated here for convenience.
Installation
The very first step is to install db4o: to do this simply copy the .jar file into an appropriate directory. The best place to put it is in your /tomcat/common/lib/ directory. Copy it there now from the course download directory: download, or use the link to the .jar file: db4o-6.4-java5.jar. You have now installed db4o!
To use it from your Java programs, you also need to ensure that it is on the Java classpath. From within Intellij, simply add the .jar file to your project. Go to project settings / project structure, then 'add classes' to add the .jar file as shown below (the image was made with a previous version of db4o).

Data Classes
For the sample application, create the data-oriented classes from the lecture notes. These are Product, MProduct (Manufactured Product), and Manufacturer. Create the package 'oodb' before entering the code for these. The complete class declarations are given below.
Note that Product and DBInterface may have a name clash with the ones in assignment 2 - they can be put into different packages to avoid this.
Saving Objects
The next piece of code creates a connection to a database file, creates some objects, saves them to the database file and then closes the connection. It is important to close the connection before exiting the Java program. Note that if the database file does not exist, then one will be created. If the file is specified as "products.yap" (.yap is a standard extension) then the file will be searched for or created in the current directory. This will depend on context in which the code is invoked! You can set this up using a 'run' configuration with Intellij, but it will default to the project root directory.
When run as a web application within Tomcat-5.5.20, it will create the file in /tomcat/bin/.
Run the above program once by invoking the main method. You can do this by right-clicking the file within Intellij and selection 'Run'.
Retrieving Objects
Now enter and run the following code to perform some retrievals. This code follows the code discussed in the lecture notes. These queries are called native queries, since native Java code is used to define a match predicate; the objects returned are those for which the predicate returns true. This gives greatly power and flexibility for constructing queries.
Try to predict the output of the program before running it, then see if your predictions were correct.
Note that as it stands, the program deletes each object after it's details are printed.
Exercise: comment out the deletions, and then repeatedly run the SaveProducts program followed by the QueryProducts program. What do you notice?
Web App
So far we've only run the save and retrieval program within in stand-alone mode. Now create the JSP page ListProducts.jsp, which simple prints the name of each product in the database within a very simple web page. The JSP is shown here as an image; you can base your code on this. This solution actually uses a DummyDB; simply exchange this for the real DB when ready (see below).
This should produce output a bit like this, depending on what data you put in:

The next step is to add thumbnail images for each product.
OODB.java
In order to make the JSP page work with live data, you need to provide an implementation of OODB.java. Note that the existing methods of QueryProducts.java assume that the retrieved objects will be accessed while a database connection is open. When working within a web application, we need to take care about when connections are opened and closed. For now, we shall open and close the connection each time it is needed. This is not the most efficient option, but it is the simplest.
Note however, that we copy the objects to a separate collection to be returned. If this is not done, trouble can arise when an ObjectFacade is returned instead of the true object.
Summary
In this lab we studied how to:
- install the db4o object database
- create some data-oriented classes
- save objects of those classes in the database
- retrieve those objects by specifying a match predicate
end of page