Your first JPA Project

I am new to JPA and here after some practice I am consolidating the steps to create your first JPA Project. But, first the a brief intro of JPA. JPA(JAVA Persistence API) is the ORM specification from JEE. Basically it is a consolidation of many ORM implementations. JPA has replaced CMP EJBs in JEE now. JPA is just the specification and there are few vendors providing implementation of this.
  • EclipseLink 
  • Openjpa 
  • Hibernate
What do you need :

  • Eclipse
  • Dali plugin for eclipse
  • Eclipselink implementation (I used eclipselink)
  • MySql
Now lets get our hands into it.
Open your eclipse and install Dali plugin first (it will be good if you take eclipse with all JEE features). Once you are done with plugin installation create a new JPA project lets say FirstJPA, here is the screen shots of project creation
in this first dialog of JPA project creation you select runtime environment and rest is default.
Now move to next dialog which is nothing just to select source folder.
The last one is the main .


In last and final dialog you select the vendor who is actual provider of JPA implementation as user library and you select the connection and you can select to create orm file here.





Click the finish and you have create your fist JPA project.
now lets create our first entity. I will create Person as the first entity. right click on your project and click on new -> other -> jpa entity. now simply click on next and you will be able to add fields to your entity




I have added four entities in this class on this dialog, just click on add and you will see this dialog where you select type of variable and name your property. for example I used id, firstName, lastName, midName and dob (date of birth).
Just click on finish.

once you are done with adding fields to your entity just make id field as your key by checking the checkbox.
Now lets add one more field in your entity Gender of enum type. Its simple just like any other class add private field, and write its setters and getters.
Now first lets create table in the database.

create table person(
id integer primary key,
first_name varchar(60) not null,
mid_name varchar(30),
last_name varchar(60) not null,
gender char(1) not null,
dob date)


now map these fields with table colums. open up the jpa details view and select one of the field in Person class lets firstName, now as you can see this will be selected in JPA  details view with default values and we know that our table column name does not match the field name so we need to provide that information in Entity.


 Just write down the table column name in column field.
One important point to mention here is that you need to map date and time fields as temporal defined in JPA api. reason is in java there is only one type for date and time but database may have fields date or datetime which we need to tell JPA. For dob select time as temporal type in type section. After all this your class will look something like this.

@Entity
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private int id;
@Basic
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Temporal(TIME)
private Date dob;
@Column(name = "mid_name")
private String midName;

private Gender gender;


now when we are done with our first entity class lets configure our persistence configuration. Open your persistence.xml and you will see tabs at bottom. In provider field put org.eclipse.persistence.jpa.PersistenceProvider as the provider and in managed classes select the entity class.

On connection tab provide all the connection parameters, and your are done. Your persistence.xml will something like this.

<persistence version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/persistence" xsi:schemalocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"></persistence>
<persistence-unit name="FirstJPA" transaction-type="RESOURCE_LOCAL"></persistence-unit>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<mapping-file>META-INF/orm.xml</mapping-file>
<class>com.first.Person</class>
<properties></properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"></property>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="javax.persistence.jdbc.user" value="root"></property>

Now lets test our code, write the test case.
Person person=new Person();
  person.setId(1);
  person.setFirstName("test_firstname1");
  person.setLastName("test_lastname1");
person.setGender(Gender.M);
person.setDob(new Date());
EntityManagerFactory fac = Persistence.createEntityManagerFactory("jpaone");
  EntityManager em = fac.createEntityManager();
  EntityTransaction txn = em.getTransaction();
txn.begin();
  em.persist(person);
  txn.commit();
  em.close();
  fac.close();

To run this test you will either have to enhance your code by implementer specific steps or some provider provides a way to run code without enhancing your code. For eclipselink you have to specify -javaagent:"/eclipselink.jar" as the vm argument which will do enhancement on the fly.


Related links
http://en.wikibooks.org/wiki/Java_Persistence/Inheritance
http://www.objectdb.com/java/jpa/getting/started
http://docs.oracle.com/javaee/6/tutorial/doc/bnbpy.html
http://help.eclipse.org/indigo/index.jsp

Comments