EclipseLink – JPA Installation And Configuration Using Eclipse

SHARE & COMMENT :

This article should teach you how can do you install and use the EclipseLink-JPA. The Integrated Development Environment that will be used in this installation guide is “Eclipse Java EE IDE for Web Developers- Kepler Service Release 1”.

Install Eclipse IDE

First of all you should install Eclipse IDE from Eclipse official site. Open the Eclipse after finishing the installation. This tutorial uses the Kepler version of Eclipse IDE. The view of the Eclipse IDE should be as shown in the figure 1-1.

EclipseIDE-Welcome

Figure 1.1

Install EclipseLink JAR Files

  • Download EclipseLink 2.5.1 Installer Zip from the eclipse organization site or follow this link https://www.eclipse.org/eclipselink/downloads/ link.  That contains the implementation for all persistence services that EclipseLink provides.
  • The JAR files that should be used for JPA persistence service are:
  1. eclipselink.jar
  2. javax.persistence.source_2.1.0.v201304241213.jar
  3. javax.persistence_2.1.0.v201304241213.jar
  • Unzip the file into any directory you would use for completing the installation. Already we have Unzip the file into “C:/JavaBeat” folder.

Set JPA Facet in Eclipse IDE

Create an eclipse dynamic project and prepare it for adding JPA facet. Follow the steps:

  1. Open an Eclipse IDE.
  2. Create a Dynamic project.
  3. Right-click on the project created. Select properties.
  4. Click on the Project Facets.
  5. Make the JPA option selected, and choose JPA version 2.1. See the figure .1.2.

Dynamic-Project-Created

Figure 1.2

A further configuration is required, so proceed to complete it by pressing on “Further configuration required” error message that shown below. This action should open a new dialog for setting a JPA Facet.

Add JPA Facet

The JPA Fact dialog should be displayed as shown at Figure 1.3. This dialog allows the user to add the required libraries to implement the JPA.

JPA-Facet

 Figure 1.3

  1. The dialog shows the platform of the persistence that will be used; it is by default select the “Generic 2.1” option. Change the platform to be “EclipseLink 2.5.x”.

  2. Let the JPA implementation menu without any change. The type “User Library” allows you to add a user driven library. And add the JPA library.
  3. Click on “Manage Libraries”. Another way of adding library can be achieved by using “Download Library” that not covered at this tutorial.
  4. Click on “New” to add a new library.
  5. Set the name of the library and click Okay.

Now, the library is ready to add the required JARs that mentioned in the “Install EclipseLink JAR files” step. See the figure 1.4.

Create-JPA-User-Library

Figure 1.4

Library Configuration

The configuration of the library require adding the needed JARs file for implementing the JPA. Follow the next steps for achieving that configuration.

  • Click on “Add External JARs”.
  • Add eclipselink.jar that’s located under “C:/JavaBeat/eclipselink/jlib”.
  • Add each JAR starting with javax.persistence.* that’s located under “C:/JavaBeat/eclipselink/jlib/jpa”. See Figure 1.5.

Add-Libraries

Figure 1.5

  • Click Okay, should close the add library dialog. But to avoid the JPA Facet: “At least one user library must be selected”.
  • Select the created library.
  • Click “Okay”.

The dynamic project created is ready now for using a JPA. But the JPA is a persistence service that relates with the Relational-Database system. So one step remaining and it is the database connection creation.

Database Connection Creation

Once you have finished creating a user library, it is now the step of creating a database connection. The final dialog should appear as you can see at Figure 1.6.

UserLibraryAdded

Figure 1.6

To create a database connection, follow the following steps:

  1. Click on “Add Connection”.
  2. Connection profile dialog will be opened.
  3. Choose the type of the database that your project would use. This tutorial assumes that the database that would be used is a MySQL database.
  4. Change the name of the connection. Click next.
  5. Fill the form of the “Specify a Driver and Connection Details”. The dialog should select the driver of the database that you had selected automatically. if it doesn’t, you have to go through to install a new driver definition; that will not be covered at this article. But when you come to the properties of the connection, you should specify the correct database connection information and then click on the “Test Connection”. See Figure 1.7.
  6. By end of the step 5, you are ready to use your connection in your JPA project.
  7. Click finish.
  8. The dialog should be closed, so be sure that the created connection has been selected.
  9. Click Okay will close the Further configuration dialog.
  10. Click Okay will close the project facets.

ConnectionTest

Figure 1.7

Now, you have created a new “Dynamic Project” and configured it for JPA use. Navigate the project created and open the persistence.xml that you have within a “META-INF” folder.

Persistence Configuration

I you have reached here, it seems everything are done successfully, so it’s a time for editing the persistence.xml that would help us to connect our database.

To configure the persistence.xml file you have to double click on it. It should be opened in a separate editor. The opened editor has multiple titled tabs shown at the bottom of the editor. Follow the below steps for full configuration of persistence.xml.

  1. Navigate to Connection tab.
  2. Change the transaction type into “Resource Local”. This change should activate the EclipseLink connection pool.
  3. Click on “populate from connection”.
  4. Choose the connection that created before.
  5. Use the Ctrl + S to save the file.
  6. Navigate to Source tab. You should see the minimal amount of configuration that required to connect your database.

See the Figure 1.8 for correct database connection.

ConnectionWithinPersistence

Figure 1.8

Testing EclipseLink Application

For testing the configuration of your project if whether it is working or not, you have to develop a Java Class. That Java Class should use the persistence name of your persistence.xml. In our case, the persistence name is “EclipseLink-JPA-Installation”.

  • Write a main method and use Persistence class to create EntityManagerFactory; that is consider as an entry point for the JPA.
  • Test you connection via calling isOpen() method from the entity manager that created before. See Figure 1.9.

TestResult

Figure 1.9

The code of the testing application:

package net.javabeat.eclipselink;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JPAImpl {
     public static void main(String [] args){
         EntityManagerFactory factory = Persistence.createEntityManagerFactory("EclipseLink-JPA-Installation");
         System.out.println("Is opened connection :: "+ factory.createEntityManager().isOpen());
     }
}

Comments

comments

About Amr Mohammed

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better