Simple JPA Application with Netbeans
March 6, 2008
Well yesterday I was demo about JPA (Java Persistent API) with Netbeans. How easy to develop simple database application using JPA with Netbeans IDE.
JPA introduction please refer to Sun Microsystem site
Ok! here the step by step about the sample application
Create a database in your database engine. For example dbperson. I’m using Mysql as the database.
Open you Netbeans IDE and try to connect to the database. Please read my other writing about “Manipulasi MySQL dengan Netbeans“
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.
Then add mysql connector to the project. Right click on Libraries–>Add Library. Find libarary MySQL JDBC Driver.
Next step we create an Entity class Person.java. Right click on Source Package New–>Entity Class
Write Person for the Class Name and entity for the Package.
Click on the Create Persistence Unit… button and chose Database Connection to the dbperson.
Click on Create button and click Finish. Add more property on Person.java for example we have name, address and phone number.
And don’t forget to add setter and getter method for that new attribute. Ok next step we create another class Demo.java
</p></p>
<p align="left">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();
}
}
}
Ok now build your 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.
Entry Filed under: Java Advance. .
24 Comments Add your own
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed









1. Abishek Kumar | April 2, 2008 at 6:58 pm
Hi Steven,
I am new to EJB3 , your blog helped me to understand how to develop JPA, thanks for it. it was easy to understand.
one more thing if you have wrote any blog or if you find any blog on developing sample EJB project with complete cyce
JSP>Servlet>SessionBeans>JPA.
please send me on abishek_indian@yahoo.com
Iam really looking for something like that, Iam trying to run, but no success yet hope i could do soon..
thanks
abishek kumar.
2. Hendro Steven | April 3, 2008 at 1:25 am
hallo Abishek Kumar,
Thanks for your comment..
wait for 1 or 2 days I’ll write a complete JEE step by step tutorial with Netbeans. I’ll show you how easy to develop struts/jsp/servlet>sessionbean>jpa application.
happy code
3. Abishek Kumar | April 6, 2008 at 11:52 pm
thanks man, i will be waiting for your blog.. plz do reply me when you are done..!
If you know please tell me answer for this..!
when we run any enterprise application on netbeans ide and glassfish server do we need to deploy any thing on glass fish..?
I mean is there any file like jboss-app.xml of Jboss server. I was following this example from this link but not able to run on glass fish..!
http://www.roseindia.net/ejb/entity-bean-example.shtml
please help me if you can..! thanks
4. Hendro Steven | April 7, 2008 at 4:22 am
mmm.. i’m veryyy buzzzzy.. this week
maybe this weekend i’ll post it 

glassfish?? you not need to add any library to running enterprise application on it. Of course if you using some library like mysql-connector or jpa hibernate implementation and etc you sould include it in your application library. jbozz-app.xml is for jboss server. It’s a specific configuration for jboss and not for glassfish
Due the example you mention above, it’s for Jboss server. If you plan to deploy it on glassfish using netbeans ide just create the classess and the jsp page and use TopLink as the jpa implementation.
5. Abishek Kumar | April 7, 2008 at 5:19 am
thanks for your clarrification. I really appretiate for your prompt replies.. it is nice.. !
will be waiting for your new blogs.!
good luck to you.
abishek kumar.
6. chester | April 17, 2008 at 6:40 am
hai mas steven, salam kenal. Saya kemarin ikut JAMU dan merasa tertarik dengan tip yang dipaparkan kemarin itu. Saya sudah mencobanya dan berhasil. Tapi saya merasa kesulitan bagaimana caranya untuk menampilkan data, misalnya ke dalam tabel. juga bagaimana caranya untuk mengupdate data dengan menggunakan klausa where xxx, trims
7. Hendro Steven | April 17, 2008 at 7:04 am
Hallo chester
salam kenal juga..
aku pikir yang lain hanya pada bengong semua hehehehe…
wah ternyata JaMu kemarin ada manfaatnya juga ya.. mantap deh
kembali ke pertanyaan kamu
aku lagi buat tulisan tentang itu bentar malam aku post.. i hope soo
8. vyor | April 17, 2008 at 8:17 am
to : Chester
Ini saya sedikit share tentang menampilkan data kedalam tabel. Saya asumsikan data yang dikembalikan dari DAO kita berupa List Of Mahasiswa. Objek mahasiswa diasumsikan field-fieldnya adalah NIM,Nama,Alamat.
Saya contohkan nama Listnya adalah “listMhs” dan nama tabel “tMhs”.
Contoh coding bisa kamu liat disini :
http://vyor.wordpress.com/2008/02/20/akses-web-service-net-dengan-java-swing/
disitu ada contoh sederhana untuk menampilkan data ke tabel
9. vyor | April 17, 2008 at 8:20 am
Bagian penting dari coding tersebut sebernya pada perulangannya.
Jika kita menggunakan asumsi tadi maka codingnya dapat kita rubah menjadi :
String[][] data = new String[listMhs][3];
int i = 0;
for(Mahasiswa m : listMhs){
data[i][0] = m.getNIM();
data[i][1] = m.getNama();
data[i][2] = m.getAlamat();
++i;
}
tMhs.setModel(new DefaultTableModel(data,
new String[]{“NIM”,”Nama”,”Alamat”}));
semoga membantu !
Salam,
10. shinobi | April 24, 2008 at 1:27 am
pak, update primary key di toplink koq ga bsa ya? errornya spt ini :
The attribute [npm] of class [Mahasiswa.npm] is mapped to a primary key column in the database. Updates are not allowed.
11. dodo | June 2, 2008 at 1:20 pm
Mas bisa minta tutorial netbeans yang utk bkin program aplikasi HP
12. dodo | June 2, 2008 at 1:33 pm
ya klo bs yg bhs indo
13. Hendro Steven | June 3, 2008 at 8:20 am
Hallo Dodo..
wah maaf kebetulan saya tidak mendalami JME (java untuk HP) tapi kalo mau contohnya sebenarnya udah ada di installasi Netbeans untuk JME. Coba aja create project baru tapi pilih Sample pasti ada deh
14. Hendro Steven | June 3, 2008 at 8:22 am
ada contoh gamenya kalo gak salah
15. Jim | July 23, 2008 at 10:07 pm
I tried your example but I got the following errors in netbeans 6.1
Jul 23, 2008 5:54:38 PM org.jdesktop.application.Application$1 run
SEVERE: Application class com.dgtlrift.test.Test failed to launch
javax.persistence.PersistenceException: No Persistence provider for EntityManager named simple-jpaPU: The following providers:
oracle.toplink.essentials.PersistenceProvider
oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider
Returned null to createEntityManagerFactory.
Any ideas or suggestions?
16. Varun | July 24, 2008 at 6:39 pm
Hi,
My name is Varun Nischal and I’m the NetBeans Community Docs Contribution Coordinator. Your blog entry would make a fantastic tutorial for our Community Docs wiki (http://wiki.netbeans.org/CommunityDocs).
Would you be willing to contribute it? If you need any help or have any questions, please contact me at nvarun AT netbeans DOT org
I look forward to hearing from you.
Thanks,
Varun Nischal
http://nb-community-docs.blogspot.com/
–
“You must do the things you think you cannot do.”
17. Krishna | July 30, 2008 at 8:42 am
Good article on JPA with NetBeans 6.1 :
JPA NetBeans
18. Fery | September 22, 2008 at 5:19 am
minta contoh2 coding spring MVC donk..
yang beginner2 aja..
trims
19. vyor | October 9, 2008 at 4:31 am
Mantap Kak !!!!
20. hendra | October 28, 2008 at 3:06 am
pak hendro kapan buat tutorial tentang design pattern?
kutunggu-tunggu
21. FemiF | January 8, 2009 at 1:17 pm
Many thanks
I have been banging my head on a wall
22. silver X | February 8, 2009 at 2:52 pm
ya mas… yang MVC-MVC itu loh….
walaupun DAO termaksud MVC jg kan???
23. Hendro Steven | February 9, 2009 at 11:03 pm
DAOMVC DAO Berhubungan dengan cara mengakses data sedangkan mvc untuk pemisahan layer model, view dan controller.. Biasanya DAO dipake disisi bagaimana mengakses Model dalam MVC
24. miftahul | October 10, 2009 at 10:39 pm
mas….kok pinter banget toh mas…..doain aku biar bisa pinter dan manfaatin ilmu kaya mas yah……..