NetBeans61HibernateJPA

NetBeans 6.1 Hibernate JPA

Contributed By; Samuel Tan


Contents

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.

File:hb1_NetBeans61HibernateJPA.PNG

File:hb2_NetBeans61HibernateJPA.PNG

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.

File:hb3_NetBeans61HibernateJPA.PNG

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

File:hb4_NetBeans61HibernateJPA.PNG

We name the library HibernateSupport. Click Ok.

File:hb5_NetBeans61HibernateJPA.PNG

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

File:hb6_NetBeans61HibernateJPA.PNG

Click Add Jar, then click Add Library.

File:hb7_NetBeans61HibernateJPA.PNG

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.

File:hb8_NetBeans61HibernateJPA.PNG

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.

File:hb10_NetBeans61HibernateJPA.PNG

File:hb11_NetBeans61HibernateJPA.PNG

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

File:hb12_NetBeans61HibernateJPA.PNG

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.

File:hbLib_NetBeans61HibernateJPA.PNG

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.

File:hb15_NetBeans61HibernateJPA.PNG

Source Codes


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

File:hb16_NetBeans61HibernateJPA.PNG

Then add the persist method;

File:hb17_NetBeans61HibernateJPA.PNG

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

File:hb18_NetBeans61HibernateJPA.PNG

Compile/Build Project


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

File:hb19_NetBeans61HibernateJPA.PNG

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

File:hb20_NetBeans61HibernateJPA.PNG

Hope you enjoyed executing this tutorial!

Thanks,


Reposted from this blog entry: http://purpleflute.juzblog.juzweb.com/2008/09/25/netbeans_hibernate/


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