JPA Demo For Beginners - Part 1

What is JPA?
 
The process of mapping Java objects to database tables and vice versa is called Object Relational Mapping(ORM). The Java Persistence API provides Java developers with an ORM facility for managing relational data in Java applications.

What are Entities?
 
A persistence entity is a lightweight Java class whose state is typically persisted to a table in a relational database. Instances of such an entity correspond to individual rows in the table. 

Entities typically have relationships with other entities, and these relationships are expressed through object/relational metadata. Object/relational metadata can be specified directly in the entity class file by using annotations, or in a separate XML descriptor file distributed with the application.

Java Persistence Query Language
 
The Java Persistence Query Language makes queries against entities stored in a relational database. Queries resemble SQL queries in syntax, but operate against entity objects rather than directly with database tables.

Evolution of JPA
 
Initial versions of EJB used entity beans (represents persistent data maintained in a database. Entity beans were superseded by the Java Persistence API and are therefore considered a deprecated technology), which were so complicated and they could be used only in Java EE application servers because of interconnections and dependencies in the source code between beans and DAO objects or persistence framework.

Due to these complications, third party persistence frameworks like Hibernatewere widely used. EJB 3.0 came with JPA, which implemented many features of third party persistence frameworks.

The EJB 3.0 specification included a definition of the Java Persistence API. However, end-users do not need an EJB container or a Java EE application server in order to run applications that use this persistence API. The Java Persistence API replaces the persistence solution of EJB 2.0 CMP (Container Managed Persistence).

JPA Project – Demo

Let me start with the database used. The database used  is IBM DB2 and I am taking a table named “COUNTRY” for our sample demo. It contains 5 columns and currently the table is empty. By using JPA we are going to build a object relationship for this table.


 Country table with 5 columns


















I am using WID 7.0, which is basically used for Webspere Process Server Application Development. We can use any IDE’s like RAD, RSA etc. for developing our first JPA project.

Steps for creating your first JPA project

Step1: You can keep Java EE perspective. On the project Explorer tab, right click and create a JPA project as below.

Creating JPA Project

Step 2: This will open up a popup for configuring the JPA project settings. Don’t worry about the target runtime. It can be WAS or any application server according to the IDE you are using. Enter your project name. Keep in mind that a single EAR will be created for the JPA and EJB project which we are going to create later.

CountryJPA Project Creation

 Step3: On click of next button, the below popup will load, where you can make connection to the database you wanted. Click on “Add Connection”. Select the database you want to connect. Here I am going to connect to my DB2 Database.

Selecting the Database
 Step 4:  Provide your Database credentials like database name, host, port number, username and password. Once you give all details, you can test the connection and in ideal case you should get a popup as “Ping Succeeded”. Your configuration settings are ready and you can click finish.

Adding DB Credentials

0 comments :

Post a Comment