SimpleJPAApplicationWithNetbeans

Simple JPA Application with NetBeans 6.0

Contributed By; Hendro Steven


Contents

Introduction


I attended a demo on JPA (Java Persistent API) with Netbeans. I was surprised to see, "How easy to develop simple database application using JPA with Netbeans IDE".

For more information on JPA, please refer to Sun Microsystems WebSite.

Getting Started


Create a database in your database engine. For example dbperson. I’m using Mysql as the database. Assuming, the Netbeans IDE is running, try to connect to the database. Please read my other writing about "Manipulasi MySQL dengan Netbeans"

File:jpa1_SimpleJPAApplicationWithNetbeans.jpg

Steps to Reproduce


Our database doesn’t have any tables. JPA will create it automatically for us. Next step we create a new Java Project in Netbeans, simple-jpa for example.

File:jpa2_SimpleJPAApplicationWithNetbeans.jpg

Add MySQL Connector

Then add MySQL connector to the project. Right click on Libraries–>Add Library. Find libarary MySQL JDBC Driver.

File:jpa3_SimpleJPAApplicationWithNetbeans.jpg

File:jpa4_SimpleJPAApplicationWithNetbeans.jpg

Creating Entity Classes

Next step we create an Entity class Person.java. Right click on Source Package New > Entity Class

File:jpa5_SimpleJPAApplicationWithNetbeans.jpg

Write Person for the Class Name and entity for the Package.

File:jpa6_SimpleJPAApplicationWithNetbeans.jpg

Create Persistence Unit

Click on the Create Persistence Unit... button and chose Database Connection to the dbperson.

File:jpa71_SimpleJPAApplicationWithNetbeans.jpg

Click on Create button and click Finish. Add more property on Person.java for example we have name, address and phone number.

File:jpa8_SimpleJPAApplicationWithNetbeans.jpg

And don’t forget to add setter and getter method for that new attribute. Now, we will create another class, name it Demo.java;

import entity.Person;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
public class Demo {
public static void main(String args[]){
Person p = new Person();
p.setName("Hendro Steven");
p.setAddress("Salatiga, Indonesia");
p.setPhoneNumber("+6281390989669");

Demo demo = new Demo();
demo.persist(p);
}

public void persist(Object object) {
EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("simple-jpaPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
em.persist(object);
em.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
em.getTransaction().rollback();
} finally {
em.close();
}
}
}

Build Project


Now, we shall build the project and run the Demo class, and the magic will show :) JPA will create your table automatically and insert our object to the table :) without any sql that we create.

File:jpa9_SimpleJPAApplicationWithNetbeans.jpg

Hope you enjoyed executing this tutorial!

Thanks,


Reposted from this blog entry: http://hendrosteven.wordpress.com/2008/03/06/simple-jpa-application-with-netbeans/


Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo