February 25, 2021
Hot Topics:

Integrate ObjectDB into Your JPA-based Java Web App

  • By Leonard Anghel
  • Send Email »
  • More Articles »

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.

Creating a New Java Web Application Skeleton

You start by creating a new Java Web application skeleton in NetBeans style. This can be accomplished by following these steps:

  1. Launch NetBeans IDE.
  2. From the File main menu, select New Project item.
  3. In the New Project wizard, select Java Web from the Categories panel and, Web Application from Projects panel. Click Next.

    ObjectDB JPA NetBeans
    Click here for larger image

    Figure 1. Creating a New Web Application, Step 3

  4. At this step, add project name as atpPlayers and select the project location. Click Next.

    ObjectDB JPA NetBeans
    Click here for larger image

    Figure 2. Creating a New Web Application, Step 4

  5. Next, select GlassFishServer 3 as the server of your application and Java EE 6 Web as the Java EE version. Click Finish.

    ObjectDB JPA NetBeans
    Click here for larger image

    Figure 3. Creating a New Web Application, Step 5

After a few seconds, a brand new empty project will be generated and appear under the Projects tab.

Add ObjectDB/JPA Support for the Project

Now is a good time to add ObjectDB/JPA support to our project before adding any business logic:

  1. For this, under the Projects tab, expand the main atpPlayers project node and localize the Libraries node.
  2. Right-click on it, and select Add JAR/Folder from the context menu.
  3. Navigate to the ObjectDB folder/bin, and select the objectdb.jar archive.

    ObjectDB JPA NetBeans
    Click here for larger image

    Figure 4. Adding ObjectDB/JPA Support

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:

  1. Under the atpPlayers project node, localize the Source Packages node.
  2. 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.
  3. In the Entity Class wizard, type Player as the entity class name, and ATP for package. Click Next.

    ObjectDB JPA NetBeans
    Click here for larger image

    Figure 5. Creating an Entity Class

  4. Next, just click the Finish button for generating the entity class and a persistence.xml file with a default persistence unit (that will be configured later).

Now, the entity class Player.java should be generated and automatically opened for edit. Just delete the generated content and paste the content below:

package ATP;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

public class Player implements Serializable {
private static final long serialVersionUID = 1L;
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private int ranking;
private String img;
private String name;
private String country;
private int points;

public Player(){

public Player(String name, String country, int ranking, int points, String img) {
this.ranking = ranking;
this.img = img;
this.name = name;
this.country = country;
this.points = points;

public Long getId() {
return id;

public void setId(Long id) {
this.id = id;

public String getCountry() {
return country;

public void setCountry(String country) {
this.country = country;

public String getName() {
return name;

public void setName(String name) {
this.name = name;

public String getImg() {
return img;

public void setImg(String img) {
this.img = img;

public int getRanking() {
return ranking;

public void setRanking(int ranking) {
this.ranking = ranking;

public int getPoints() {
return points;

public void setPoints(int points) {
this.points = points;

public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;

public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Player)) {
return false;
Player other = (Player) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
return true;

public String toString() {
return "ATP.Player[id=" + id + "]";

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:

 <?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"

<persistence-unit name="atpPlayersPU" transaction-type="JTA">
<property name="javax.persistence.jdbc.url" value="$objectdb/db/atp.odb"/>
<property name="javax.persistence.jdbc.user" value="admin"/>
<property name="javax.persistence.jdbc.password" value="admin"/>


You should obtain something similar to Figure 6 below.

ObjectDB JPA NetBeans
Click here for larger image

Figure 6. The Persistence Unit

Page 1 of 2

This article was originally published on April 6, 2012

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

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