Thursday, July 4, 2013

Introduction to Entity beans in Java Persistence API (JPA)

Hi All,

In this post we will discuss about Java Persistence API(JPA) entity bean.

Lets have a brief introduction about JPA.
JPA is a Java data base framework which eases interaction with database by providing following features
  1. Java Persistence Query language (JPQL)
  2. Object relational mapping
  3. Providing annotation etc.
The most important aspect in JPA is a Java bean class .
Bean in Java is a simple class with properties and their getter and setter methods

In JPA we call this bean with a special name called as Entity.

Entity Bean :- is an exact replica of a table in a database,it is independent of database used.
  • The mapping between a Entity Bean and data base table is configured in persistence.xml file
  • There are different types of annotations(@) which helps in representing primary keys,table name etc.
  • Annotation are in simple metadata(Data about Data),they give extra information to compiler
  • A normal Java bean can be converted into a Entity by using @Entity annotation
we will discuss more about annotations in upcoming posts.

In this post we will see how to write a Entity Bean

Environment used :-
  • Eclipse Juno
  • Oracle
  • JPA 2.0 jars
Lets have below COMPANY table in data base

    SNO
  • NAME
    MAIL
    1
  • Google
  • google@gmail.com
    2
  • Yahoo
  • yahoo@yahoo.com
    3
  • Microsoft
  • microsoft@live.com

Now we will write a Entity class for the same. In Eclipse its simple to create most of the required code will be auto generated.
  1. Switch to JPA perspective in Eclipse
  2. Configure data base connection by using Data Source Explorer view(window-->Show view-->Data Source Explorer)
  3. Create a JPA project(File—New-->Create JPA project)
  4. Open the project,in JPA content you will find persistence.xml file
  5. Now select the project click on new-->others--JPA Entity from tables
  6. Select the data base connection , schema configured in step 2 and select tables that you want to generate Entities
  7. Click the check box-- update in persistence.xml
  8. Select a primary key generator type like Auto etc(Not mandatory) continue and click finish
Lets see the code that gets generated for COMPANY table


@Entity
public class Company implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private BigDecimal sno;
private String mail;
private String name;
public Company() {
}

public String getMail() {
return this.mail;
}

public void setMail(String mail) {
this.mail = mail;
}

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

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

public BigDecimal getSno() {
return this.sno;
}

public void setSno(BigDecimal sno) {
this.sno = sno;
}

}


Explanation :-

Here we need to take a look at two annotations

  1. @Entity :- Gives a identification for a normal Java bean as entity
  2. @Id :- Indicates the primary key in the table

Here we have three properties and their getter and setter methods,we will be accessing (setting/retrieving )the properties by using these methods.

There are lot more annotations available for various purposes will explain in details in my upcoming posts.

Now have a look at persistence.xml, we will find the Entity class added.

                         Happy Learning

Please provide your valuable comments on this article and share it across your network.


No comments:

Post a Comment

Like and Share