Revision as of 14:49, 5 November 2009 by Admin (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)

NetBeans 6.1 Hibernate JPA

Contributed By; Samuel Tan


Brief Overview

Google for the whole day but could not found any information on how to setup Netbeans 6.1 to use Hibernate JPA implementation sample. All samples found were using TopLink. Since with some try and error I finnaly get Hibernate working in this environment, I think it will be wise to share with the public.

Getting Started

Let us create a simple JPA Java Application project with Netbeans.



Setup Hibernate Library

Next step is the critical one. We will setup the Hibernate library we going to use as the JPA implementation. I have prepared a set of the Hibernate library here (12MB). Just download it and unzip it under the project lib folder, and follow these steps. Right click on the library folder in the projects tab and select the Add library option.


We will create our own set of library. Click on the Create button.


We name the library HibernateSupport. Click Ok.


Select all the jar files in the hibernate folder under the project lib we just unzip.


Click Add Jar, then click Add Library.


Import MySQL Java Driver

Next, we need to import MySQL Java driver as we are using MySQL database. By default, Netbeans should already have the driver, we just need to import it to our project.


Next Go the the Service Tab and create a Database connection to MySQL database. It is assume you know how to do this. Otherwise please refer to here. However, just follow the steps until the database is created. You do no need to create the table as JPA will do that for you.

Creating Entity Classes

After you created the connection to MySQL, we need to create the entity object which we called it Person. With netbeans, we use the entity class object creation wizard that save us from hassles to create the persistence.xml.



Enter the class name as Person and package as simpleJPA.entity. Then click on the Create Persistence Unit button.


Setup Persistence Library

Click Table Generation Strategy as Create and select the database connection you just setup. Select New Persistence Library for the Persistence Library field and you will be prompted for the library that pointed to the Hibernate Entity Manager.


Give the library a name and select those libraries as shown in above picture under the hibernate folder you have unzipped. Click OK and Create. The Person object/entity is now generated for you. For it to work, we just add name, address and phone to the person so we can interact with it. Add in the field member as shown and then use the shortcut Alt-Ins to generate the getter and setter.


Source Codes

Then go to our Main class and add the following code;


Then add the persist method;


The last step, we need to add file to our source folder.


Compile/Build Project

Then Compile and build our project and run it. When you run, you should see some output such as;


Then check on the database, you should have the person table created and the superman record;


Hope you enjoyed executing this tutorial!


Reposted from this blog entry:

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