Open SourceIntegrate ObjectDB into Your JPA-based Java Web App

Integrate ObjectDB into Your JPA-based Java Web App

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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

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

    ObjectDB JPA NetBeans

  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

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

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

  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;

@Entity
public class Player implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@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;
}

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

@Override
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;
}

@Override
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_xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi_schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<persistence-unit name="atpPlayersPU" transaction-type="JTA">
<provider>com.objectdb.jpa.Provider</provider>
<properties>
<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"/>
</properties>
</persistence-unit>

</persistence>

You should obtain something similar to Figure 6 below.

ObjectDB JPA NetBeans

Developing an EJB Session Bean

It is time to add some business logic to our application. Start with the database operations that will be placed into an EJB session bean. Begin by generating an empty EJB session bean, as follows:

  1. Under project main node, right-click on the Source Packages and select New | Session Bean. If Session Bean is not present, then select Other, and under Categories, select J2EE. In the right tab, you will see the Session Bean option.
  2. In the New Session Bean wizard, type “atpDAO” as the session bean name, and press Finish.

Now, the generated stateless bean code should be replaced by the code below. We have a method to persist data into database, named persist(), and one for getting all the records from the database, named getAllPlayers():

 package ATP;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

@Stateless
public class atpDAO {

// Injected database connection:
@PersistenceContext private EntityManager em;

// Stores a new player
public void persist(Player player) {
em.persist(player);
}

// Retrieves all the players
public List<Player> getAllPlayers() {
TypedQuery<Player> query = em.createQuery("SELECT p FROM Player p ORDER BY p.id", Player.class);
return query.getResultList();
}

}

Using this approach has multiple advantages because the application server will manage the instantiation of the EJB class and inject an instance of the EJB class into the Servlet (see next section), prepare an EntityManager automatically and inject it into the em field (because it is annotated with the @PersistenceContext annotation), and handle transactions automatically using JTA. There’s no need to wrap every operation that modifies the database with transaction begin and commit. For this, you need an application server that supports EJB, such as GlassFish or JBoss, and not a Servlet container, such as Tomcat and Jetty. I tested it on GlassFish v3, which comes bundled with NetBeans IDE.

Developing a Servlet Class to Manage HTTP Requests

Next, you need a component to deal with Web requests and to communicate with the EJB component. A Servlet can be a good choice in this case. To create one, you need to follow next steps:

  1. Under project main node, right-click on the Source Packages and select New | Servlet.If Servlet is not present, then select Other, and under Categories, select Web.In the right tab, you will see the Servlet option.
  2. In the New Servlet wizard, type “atpServlet” as the Servlet name, and ATP as the package. Click Finish.

ObjectDB JPA NetBeans

Now, the Servlet content must be replaced with the content below:

 package ATP;

import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "atpServlet", urlPatterns = {"/atpServlet"})
public class atpServlet extends HttpServlet {

// Injected DAO EJB
@EJB
atpDAO ejbDao;

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {

String name = request.getParameter("name");
String country = request.getParameter("country");
String sranking = request.getParameter("ranking");
String spoints = request.getParameter("points");
String photo = request.getParameter("photo");

if ((name != null) && (country != null) && (sranking != null) && (spoints != null) && (photo != null)) {
int ranking = Integer.valueOf(sranking);
int points = Integer.valueOf(spoints);
ejbDao.persist(new Player(name, country, ranking, points, photo));
}

// Display the list of guests:
request.setAttribute("players", ejbDao.getAllPlayers());
request.getRequestDispatcher("/atp.jsp").forward(request, response);
} finally {
out.close();
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}

Notice that the database operations are performed by the atpDao session bean, which is automatically instantiated and injected by the application server into the ejbDao field, because that field is marked with the @EJB annotation. Now, when your Servlet receive a HTTP request, it checks to see if this is a request for storing a new ATP player into the ObjectDB database. If the player’s parameters were set, then the EJB business method persist()method is called for storing the new player. After that, the Servlet call the EJB getAllPlayers() business method, and forwards control to a JSP page that renders the ATP players as you can see in the next section. If a new player was inserted then it will be displayed by the JSP.

Developing a JSP Page for Output Rendering

The final component of your Web application is a JSP page that renders ATP players on screen. For this, you need to follow next steps:

  1. Under project main node, right-click on the Web Pages and select New | JSP. If JSP is not present, then select Other, and under Categories, select Web. In the right tab, you will see the JSP option.
  2. Type atp as the JSP name, and press Finish.

ObjectDB JPA NetBeans

Next, replace the default generated code with the code below:

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.*,ATP.Player"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ATP Men's Rankings</title>
</head>
<body>
<table align="center">
<tr>
<%
List<Player> players = (List<Player>) request.getAttribute("players");
if (players != null) {
for (Player player : players) {
%>
<td>
<table>
<tr>
<td rowspan="5">
<img src="images/<%= player.getImg()%>" alt="">
</td>
</tr>
<tr>
<td>
Ranking:<b> <%= player.getRanking()%> </b>
</td>
</tr>
<tr>
<td>
ATP points:<b> <%= player.getPoints()%> </b>
</td>
</tr>
<tr>
<td>
Name:<b> <%= player.getName()%> </b>
</td>
</tr>
<tr>
<td>
Country:<b> <%= player.getCountry()%> </b>
</td>
</tr>
</table>
</td>
<%
}
%>
<%
}
%>
</tr>
</table>
<form method="POST" action="atpServlet">
<table align="center">
<tr>
<td colspan="2">
Insert a new player:
</td>
</tr>
<tr>
<td>
Name:
</td>
<td>
<input type="text" name="name" />
</td>
</tr>
<tr>
<td>
Country:
</td>
<td>
<input type="text" name="country" />
</td>
</tr>
<tr>
<td>
Ranking:
</td>
<td>
<input type="text" name="ranking" />
</td>
</tr>
<tr>
<td>
Points:
</td>
<td>
<input type="text" name="points" />
</td>
</tr>
<tr>
<td>
Photo:
</td>
<td>
<input type="text" name="photo" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add" />
</td>
</tr>
</table>
</form>
</body>
</html>

On the top side of the Web browser window, the JSP renders the current players from the database. On the bottom side, it renders a form for storing a new player. When you store a new player, you need to specify the name of an image file that represents a player’s photo. Normally, that photo should be uploaded, but since this is not relevant here, you can use the photos from /images folder where they were stored manually.

A final adjustment should be made – under the Web Pages node, you have the application start page, called index.jsp. Just edit it and add a forward to your Servlet, like below:

 ...
<body>
<jsp:forward page="atpServlet" />
</body>
...

Running the Application

Running the application is very simple! Make sure that atpPlayers is the main project and click the big green Run button from NetBeans toolbar, or press F6. After GlassFish is started and the application is successfully deployed, the browser should automatically open. In Figures 9 and 10, you can see two screenshots of your application.

ObjectDB JPA NetBeans
Screenshot 1 of Your Application
ObjectDB JPA NetBeans
Screenshot 2 of Your Application

Browsing the ObjectDB Databases

You can browse the ObjectDB databases by using the ObjectDB Explorer tool available in /bin folder of ObjectDB location, as explorer.exe. Double-click it to launch the application, and navigate to the atp.odb database. If you use the same paths as in this article, then atp.odb is in D:appatpPlayersbuildwebWEB-INFdb folder. In figure 11 you can see how I have browsed it:

ObjectDB JPA NetBeans

You can find out more about ObjectDB tools at ObjectDB.com.

Summary

In this article, you have learned how to develop a Web application based on ObjectDB and JPA. You saw how to download and configure ObjectDB, how to query an ObjectDB database and how to browse a database content using ObjectDB tools.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories