Netbeans 6.1 Hibernate JPA

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.

Let us create a simpleJPA Java Application project with Netbeans.

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.

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.

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.

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.

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

Then add the persist method:

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

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:

 

4 Responses to “Netbeans 6.1 Hibernate JPA”

  1. Hi,
    My name is James Branam and I’m the NetBeans Community Docs Manager. 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 james.branam@sun.com. I look forward to hearing from you.

  2. Hello - nice tutorial on JPA/netbeans setup.

    Where does the name of your blog come from? “De Purple FLute” ?

  3. Purple Flute is a type of flute made of purple bamboo. It comes from there. :D

  4. Great post! I recommend it!

Leave a Reply

You must be logged in to post a comment.