November 26, 2014
Hot Topics:

Building JPA 2 Applications Using EclipseLink in Java EE, Page 3

Building JPA 2 Applications Using EclipseLink in Java EE

In a Java EE environment, we use GlassFish V3, a Java EE 6-compliant server that provides the environment to persist the data on to the database.

Step 1: Define a New Server

To add GlassFish V3 server, go to the Server tab in Eclipse, add a new server and then download the additional server adapters in the New Server dialogue box as shown in the Figure 5.

Adding a New Server
Figure 5:
Adding a New Server

When the adapters are displayed as shown in the Figure 6, select the GlassFish server, complete the installation and provide the Server directory.

Installing New Extension, GlassFish
Figure 6:
Installing New Extension, GlassFish

Step 2: Creating a Dynamic Web Application

After GlassFish V3 is added and configured, create a new Dynamic Web Application and select the GlassFish Server in the target runtime as shown in the Figure 7.

Creating a New Dynamic Web Project and Selecting the Target Runtime
Figure 7:
Creating a New Dynamic Web Project and Selecting the Target Runtime

Step 3: Adding JPA Facet to the Web Application

For a Java EE application, we create a connection pool on the server by configuring it in the Server console (Admin Console). For the demo application, we have created a pool named jpa2Pool. After the pool is created, add JPA in the Project Facet within the properties to enable JPA (as shown below in Figure 8).

Adding JPA in the Project Facets
Figure 8:
Adding JPA in the Project Facets

Step 4: Configuring the Application in Persistence.xml

In the persistence.xml file, select JTA as the transaction type and provide the JTA data source name in the database of the Connection tab.

Figure 9: Connection Information in Persistence.xml File
Figure 9:
Connection Information in Persistence.xml File

Here is the persistence.xml file for this application:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">

<persistence-unit name="JPATestWebApp" transaction-type="JTA">


You can see that it is simpler in the Java EE environment; it contains only the data source name in the jta-data-source element of the persistence unit. This simplification is possible because the connection properties are already configured on the server side.

Step 5: Writing the Client Code

The client code (Servlet code) also will be different in the Java EE environment because now the EntityManager instances are managed by the server runtime and the application can look up the EntityManager and perform the required operation. We use the @PersistenceContext annotation by providing the persistence unit name, which configures the EntityManager instances for this unit.

import javax.annotation.Resource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.persistence.PersistenceContext;
import javax.servlet.annotation.WebServlet;
//Other imports
@PersistenceContext(name = "persistence/LogicalName",
unitName = "JPATestWebApp")
public class StudentServlet extends HttpServlet {
private javax.transaction.UserTransaction utx;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
Context ctx = (Context) new InitialContext()
EntityManager em = (EntityManager) ctx
Student myStudent = new Student(21, "Mahalakshmi", 25);
} catch (Exception ex) {
// Other code

Tags: EclipseLink, JPA 2

Originally published on

Page 3 of 4

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

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

Sitemap | Contact Us

Rocket Fuel