Wednesday, December 19, 2007

Fastest Steps to CRUD(Create,Read,Update,Delete) Database using JPA in Netbeans 6 for Super Duper Dummies

Using JPA is very easy , I mean we can using simple instruction ( just essential / basic ) only. I just try help somebody that extremely unfamiliar in Java environment. This step maybe makes 'expert' boring to hear, but I think , I have rensponsibility to help other.
In this topics, no step needed to access database directly. Creating Table handled automatically. All you need is using Netbeans 6.

PREPARATION
First, create Java Application Project. Create New Project -> Categories : Java , Project : Java Application -> Project Name : JavaApp (whatever you want to named).
Leave checked option in : Set As Main Project, and, Create Main Class : javaapp.Main
Finished create Project.

Let's start Database (Derby) ,using : Tools -> Java DB Database -> Start Server.

Now, Create Entity.
At the Project Tab , choose JavaApp project, right click, choose : New -> Entity Class.

At The bottom of Modal Windows, you can see button : Create Persistance Unit. Please click this.
On the "Database Connection" please choose databse connection :
jdbc:derby://localhost:1527/sample[app on APP].

Leave Generation strategy : Create.

Then click Button "Create".

Fill Class Name, in example : Persons.
Package, for example : mypackage.


Change Primary Key Type from : Long , into : int.

Click Finish.

Now, we will add Derby driver.
From the Left Panel, Right Click Java App project, Choose: Properties.

Choose Categories : Libraries.
On the Right panel, Add Library -> Java DB Driver. Then click Add Library Button.
On the Modal windows of Project Properties, click OK.

Now , in this step mean, we have Derby driver to make connection to Derby Server.

CODING in ACTION.
Load Person.java with double click Button,

You will see :
@Entity
public class Persons implements Serializable {

Just insert below @Entity with : @Table, so the code become :
@Entity
@Table
public class Persons implements Serializable {

If eror exist, then : Right Clik anywhere, choose Fix Import, it will importing :
import javax.persistence.Table;

Before : private int id; please add : @Column, so the code become :

@Column
private int id;

If error happen, Just Right Click, and Fix Import.
If Error happend again, I think this class need : Follow assist (click th Error Sign, Right side of code)
Choose Unify field access.

Now , for example, we Add column: firstName, write code :
private String firstName;

Double click at the firstName , Rhight clik , it will shown pop up , Choose : Insert Code -> Getter And Setter.
Check the property (firstName) then CLick : Generate.

Like Step before, we add annotation : @Column before property, then code like this :
@Column
private String firstName;


Now we load Main.java from package : javaapp. Just double click Main.java on the Left Panel (Project Tab).

We looking code :
public static void main(String[] args) {

Then, just Enter.
Right Click between bracket, we'll see : Persistance -> Use Entity Manager.

You can see, that Editor add this code :
public void persist(Object object) {
...
..

CREATE
Now, back to the Main Method, let's code :

Persons pers = new Persons();
pers.setId(1);
pers.setFirstName("bagusalfa");

persist(pers);

If error happen, because persist Method is not static,then add "static" keyword,
changed into :
public static void persist(Object object) {

Now, Run Main Project ...

Well done, you've got CREATE operation with JPA.

READ
now , add this code (just copy paste from "persist" Method) :

EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("JavaAppPU");
EntityManager em = emf.createEntityManager();

Now, we are doing Find object :
Persons findPersons=(Persons) em.find(Persons.class, pers.getId());
System.out.println(findPersons.getFirstName());

Well done,you've got READ operation.


UPDATE
now we will change Name persons.
// TODO code application logic here
Persons pers = new Persons();
pers.setId(1);
pers.setFirstName("bagusalfa");

persist(pers);
EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("JavaAppPU");
EntityManager em = emf.createEntityManager();

Persons findPersons=(Persons) em.find(Persons.class, pers.getId());
System.out.println(findPersons.getFirstName());

pers.setFirstName("alvax");
em.getTransaction().begin();
em.merge(pers);
em.getTransaction().commit();

DELETE
We will delete Person object:
EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("JavaAppPU");
EntityManager em = emf.createEntityManager();

Persons findPersons=(Persons) em.find(Persons.class, pers.getId());
System.out.println(findPersons.getFirstName());


em.getTransaction().begin();
em.remove(pers);
em.getTransaction().commit();

No comments: