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.

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.

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.

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).

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
Here is the persistence.xml file for this application:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 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="JPATestWebApp" transaction-type="JTA">
<jta-data-source>jdbc/jpa2Pool</jta-data-source>
<class>com.demo.infosys.Student</class>
</persistence-unit>
</persistence>
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
@WebServlet("/StudentServlet")
@PersistenceContext(name = "persistence/LogicalName",
unitName = "JPATestWebApp")
public class StudentServlet extends HttpServlet {
@Resource
private javax.transaction.UserTransaction utx;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try {
Context ctx = (Context) new InitialContext()
.lookup("java:comp/env");
utx.begin();
EntityManager em = (EntityManager) ctx
.lookup("persistence/LogicalName");
Student myStudent = new Student(21, "Mahalakshmi", 25);
em.persist(myStudent);
utx.commit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
// Other code
} Page 3 of 4
