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).
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
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.
Labels:J2EE
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment