Saturday, July 5, 2014
Today I am going to discuss with the first step of JPA - Java Persistence API. JPA helps you in object-relational mapping and reduces a lot of task of coders, otherwise they have to write a lot of JDBC codes. All those things are done automatically by the Persistence provider. Throughout all the topics related to JPA that we will cover will consider EclipseLink as the JPA provider.
    After all necessary setups, the first thing you will have to do is to write a JPA entity. Many IDE like Eclipse can automatically create it, but we will show how to write it and what are the steps. You must have seen the JavaBean style classes. Here our first task is to write a class in that style only. Next we will make some modifications to make it a JPA entity.
We will create a class named Employee. But you must remember that the entity name must match with the name of a table in database (though it can differ, we will only deal with name of table as same as entity).
public class Employee {
 private int id;
 private String name;
 private double salary;
 
 public int getId() { return id; }
 public void setId(int id) { this.id = id; }
 
 public String getName() { return name; }
 public void setName(String name) { this.name = name; }
 
 public double getSalary() { return salary; }
 public void setSalary(double salary) { this.salary = salary;}
}
After our task of writing the bean class is over, we will have to create a table named Employee with column names as same names as the attributes of the Employee class.
CREATE TABLE Employee
            (id number primary key,name varchar(30),salary float);
Our next task is to modify our bean class to make it a JPA entity. To do this, we have to first annotate the class with the @Entity annotation. This is just a marker annotation to notify to the persistence engine that it is a JPA entity
       Next we have to mention the primary key as every entity has a primary key and we also have a primary key in our database table. In order to mention the primary key, we have to annotate the field id with the @Id annotation.
import javax.persistence.*;

@Entity
public class Employee {
 @Id private int id;
 private String name;
 private double salary;
 
 public int getId() { return id; }
 public void setId(int id) { this.id = id; }
 
 public String getName() { return name; }
 public void setName(String name) { this.name = name; }
 
 public double getSalary() { return salary; }
 public void setSalary(double salary) { this.salary = salary;}
}
When the @Id annotation is placed on the field or property, user can choose to annotate either the declared field or the getter method of a JavaBean-style property. Either field or property strategy is allowed, depending on the needs of the entity developer. We will discuss the details of annotating persistent state using field or property access later.
       The fields in the entity are automatically made persistable by virtue of their existence in the entity. Default mapping and loading configuration values apply to these fields and enable them to be persisted when the object is persisted. The entity is automatically mapped to the table Employee and the fields are mapped to the respective columns.

0 comments:

Post a Comment

Total Pageviews

Subscribe via Email

Followers


Popular Posts

About Me

My photo

I am a student of BTech Computer Science Engineering from RCCIIT,Kolkata. I am a crazy lover of Java and wants to settle as a Java developer. I have a seven years Java experience with an application developer experience for 2 years. Recently from March 2012 I am a registered S40 app developer for Nokia and has corrected an app of them. I am currently writing blogs to encourage and grow interest in all those who don't know or learning Java.