Integrate ObjectDB into Your JPA-based Java Web App

In this article, you will learn how to integrate ObjectDB into a Java Web application based on the Java Persistence API (JPA) and NetBeans. If this is the first time you have heard about ObjectDB, it is an object database for Java applications using JPA. The ObjectDB website claims that the product "is about 10 times faster than other JPA/DBMS solutions." However, as you will see, ObjectDB has many other attributes that that make it an excellent choice for the database layer of your Java desktop or Web application. It's easy to configure and use, very robust, and requires no JDBC, drivers or third-party applications. ObjectDB is actually a powerful database with full JPA support in one product.

By the end of the article, you will be able to download and configure ObjectDB into a JPA-based Web application with NetBeans.

Downloading and Configuring ObjectDB

Begin by downloading ObjectDB from ObjectDB.com. I have used ObjectDB version 2.3.7, which comes as a ZIP archive. After download, extract the archive content to your favorite place. Depending on when you read this article, you may want to download the latest release.

ObjectDB contains a set of configurable parameters for general functions, logging, database management, entity management, schema update, server configuration, server user list and SSL. Obviously, all these parameters have default values, and it is unlikely that you will need to customize each of them. For a quick look, you can see them in the objectdb.txt file under the ObjectDB root folder. Each section is delimited by a suggestive XML tag and contains several parameters.

The most commonly modified parameter is probably the <data> element, under the server configuration section, which is marked by the <server> element. The <data> element has one attribute, path, which specifies the location of ObjectDB databases that the server manages. The value of $objectdb> (the ObjectDB home directory) is derived from the location of the objectdb.jar file. It is defined as the path to the directory in which objectdb.jar is located, with one exception -- if the name of that directory is /bin, /lib or /build the parent directory is considered to be the ObjectDB home directory ($objectdb>). You can also define $objectdb> explicitly by setting the "objectdb.home" system property:

System.setProperty("objectdb.home", "/mydatabase/odb");

Note: Beside ObjectDB, you will also need Java 1.6/1.7 and NetBeans IDE, preferably the latest release with GlassFish v3 bundled.

Putting ObjectDB into a NetBeans/JPA Application

In this section, you will develop a NetBeans/JPA/ObjectDB Web demo application. This application will render a set of ATP tennis players on the website home page, and will provide a form that can be used to record more players into the ObjectDB database.

Done! Very simple and fast! ObjectDB is set up, and now you can start developing the application.

Create an Entity Class

To store objects in an ObjectDB database using JPA, we need to define an entity class. This simple step can be accomplished with NetBeans help:

Under the atpPlayers project node, localize the Source Packages node.

Right-click on this node and select New | Entity Class from context menu. If Entity Class is not present, then select Other, and under Categories, select Persistence. In the right tab, you will see the Entity Class option.

In the Entity Class wizard, type Player as the entity class name, and ATP for package. Click Next.

As you can see, the entity class maps the player properties, like name, country, points, ranking and a photo link. These properties represent the table database columns that will be named Player in the atp.odb database.

Define a Persistence Unit Accordingly with ObjectDB

If you look under the Configuration Files node, you will notice that NetBeans has generated a default persistence unit. Open it in XML mode, and replace the current content with the content below:

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.

Thanks for your registration, follow us on our social networks to keep up-to-date