Sunday, February 24, 2013

Part 8: How to create entity classes using JPA 2.0 and EclipseLink 2.3 in Eclipse Indigo 3.7.1


Previously, we have installed and configured MySQL database with Eclipse. In this step we'll use JPA 2.0 and Eclipselink 2.3 to create entity class for our project. So, lets start.
In order to use JPA capabilities in your project, you have to add JPA Facet to your Project Facets.
Right-click your project in Project Explorer and click Properties. In the Project Facets, on the left-hand side click on Project Facets and from the Project Facets’ list check JPA. This will prompt a linkFurther Configuration required in lower part of the window. Click on Further configuration required.
In the JPA Facet Window select EclipseLink 2.3.x under Platform. In JPA implementation Click onDisable Library Configuration. Glassfish 3.1.1 Server ships with EclipseLink, so you don't have to configure it.
Note: If you are using previous versions of Glassfish; i.e, Glassfish 3.0.1 Server then you would have to download EclipseLink library and copy following jar files to glassfish3/glassfish/lib folder:
javax.persistence_2.0.3.v201010191057.jar
javax.persistence_1.0.0.jar
eclipselink.jar
If you are using Glassfish 3.1.1 Server as in this tutorial than you don't need to do anything accept selecting it from list as described above.
Under Connection Select Test MySQL database which you have previously created in the previous part. Also, check 'Add driver library to build path'. Finally it should look Like following figure; click Ok. Finally in the Project Facets click Ok and you are done.
Next, we'll create data model for our project in which we'll create entity class from table customer, which we had created in the previous part. This is called ORM (Object Relation Mapping). ORM is handled by EclipseLink.
In the project explorer, right-click on scr folder under Java Resources, and click New -> Package. Give the package name datamodel.entites and click Finish.
Right-click on datamodel.entities package and click New -> Other. From the Wizard list under JPA click JPA Entities form Tables
In Select Tables window, click on connect button as highlighted in the figure. Select your table. CheckUpdate class list in persistence.xml and click Next. Click Next again.
In the Key generator field select identity from the list. This is important to select your key generator as Identity, this will cause your entity class to automatically generate primary key. If you do not specify you key generator as identity, your application must initialize your primary key before attempting to persist any entities in database. Leave other fields as default and click finish.
The Customer.java class is created under the package datamodel.entities. The code look like the following:
Customer.java


package datamodel.entities;

import java.io.Serializable;
import javax.persistence.*;


/**
 * The persistent class for the customer database table.
 * 
 */
@Entity
public class Customer implements Serializable {
 private static final long serialVersionUID = 1L;

 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)
 @Column(name="customer_id")
 private int customerId;

 private String address;

 private int age;

 private String email;

 private String name;

    public Customer() {
    }

 public int getCustomerId() {
  return this.customerId;
 }

 public void setCustomerId(int customerId) {
  this.customerId = customerId;
 }

 public String getAddress() {
  return this.address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

 public int getAge() {
  return this.age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public String getEmail() {
  return this.email;
 }

 public void setEmail(String email) {
  this.email = email;
 }

 public String getName() {
  return this.name;
 }

 public void setName(String name) {
  this.name = name;
 }

}

Now that we have created our entity class. We'll now create our business logic, in which we'll be creating our enterprise java beans called EJBs. Before we create our EJBs, we have to create a JNDI data source name, So click Next to see how we create it.

No comments:

Post a Comment