June 21, 2018
Hot Topics:

Building JPA 2 Applications Using EclipseLink in Java SE, Page 2

Building JPA 2 Applications Using EclipseLink in Java SE

Developing a JPA 2 application that uses EclipseLink as the persistence provider does not involve much overhead. In this section, we describe the steps for building such applications with Eclipse and the MySQL database in Java SE. We do not focus on any of the trivial concepts.

Let's begin with a simple STUDENT table structured as follows:


id - int

name - String

age - int

Step 1: Create a JPA Project

The first step when building the JPA 2 application in Java SE is to create a JPA Project in Eclipse and select EclipseLink 2.0.x in the platform as shown in Figure 1. Next, open the project in the JPA perspective and add the necessary MySQL driver class.

Figure 1: Selecting the Platform in the JPA Facet
Figure 1:
Selecting the Platform in the JPA Facet

After the project is opened in the Project Explorer tab, its structure looks like Figure 2. The JPA content defines the persistence.xml configuration file.

Figure 2: JPA Project Structure
Figure 2:
JPA Project Structure

Step 2: Create the Entities by Providing All the Necessary Connection Details

The next step is to create the required entities. We chose to create entities from the table by selecting that option under JPA as shown in Figure 3. For our application, we need only one entity class: Student.

Creating Entities from a Table
Figure 3:
Creating Entities from a Table

The entity classes are the primary programming artifact required for JPA programming. Entity classes can be created by annotating the class with the @Entity, @Embeddable and @MappedSuperClass annotations defined in javax.persistence package. Here is the Student entity class:

public class Student implements Serializable {
private Integer id;
private String name;
private Integer age;
private static final long serialVersionUID = 1L;
// public Constructor
// public Setters and Getters

When creating the entity classes, it is essential to provide the connection details. If the connection does not exist, a new connection should be added by clicking the icon next to the Connection drop down. After adding the connection, select the table for which the entities should be created as shown in Figure 4. After selecting the table, click the Finish button, which creates the respective entity classes.

Select Tables from the Database Connection
Figure 4:
Select Tables from the Database Connection

Step 3: Configuring EclipseLink in the Persistence.xml File

The next task is to configure the EclipseLink in the persistence.xml configuration file. For a detailed description of the properties that have changed in the persistence.xml file for JPA 2, refer to the article The JPA 2 Enhancements Every Java Developer Should Know.

For EclipseLink to be used as the provider, it is mandatory to include org.eclipse.persistence.jpa.PersistenceProvider as the value for the provider element in the persistence unit. The persistence.xml file also has to configure the data source and provide definitions for all the entity classes defined in the persistence context. Here is a sample persistence.xml file:

<?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="JPATestProj" transaction-type="RESOURCE_LOCAL">
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpa2"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="infosys"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>

Step 4: Write the Client Code

After the Student entity class is created, the next step is to write the client code to insert the Student record using the javax.persistence.EntityManager instance. EntityManager instances manage the entities and are used to create, remove and find the persistent instances. EntityManager instances are obtained from the javax.persistence.EntityManagerFactory factory object. The entities are stored in the database using the javax.persistence.EntityTransaction instance. The client code is as shown below:

EntityManagerFactory emf = Persistence .createEntityManagerFactory("JPAProject");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
// Create the instance of Student Entity class
Student student = new Student(147675, "Afshan", 29);
// JPA API to store the Student instance on the database.


Originally published on https://www.developer.com.

Page 2 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.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date