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.
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 log4j.properties 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!
Thanks,
Reposted from this blog entry: http://purpleflute.juzblog.juzweb.com/2008/09/25/netbeans_hibernate/
Attachments
- Media:hb2_NetBeans61HibernateJPA.PNG
- Media:hb19_NetBeans61HibernateJPA.PNG
- Media:hb1_NetBeans61HibernateJPA.PNG
- Media:hb5_NetBeans61HibernateJPA.PNG
- Media:hb15_NetBeans61HibernateJPA.PNG
- Media:hb10_NetBeans61HibernateJPA.PNG
- Media:hb6_NetBeans61HibernateJPA.PNG
- Media:hb11_NetBeans61HibernateJPA.PNG
- Media:hb17_NetBeans61HibernateJPA.PNG
- Media:hb7_NetBeans61HibernateJPA.PNG
- Media:hb20_NetBeans61HibernateJPA.PNG
- Media:hb16_NetBeans61HibernateJPA.PNG
- Media:hb18_NetBeans61HibernateJPA.PNG
- Media:hb12_NetBeans61HibernateJPA.PNG
- Media:hb4_NetBeans61HibernateJPA.PNG
- Media:hb8_NetBeans61HibernateJPA.PNG
- Media:hbLib_NetBeans61HibernateJPA.PNG
- Media:hb3_NetBeans61HibernateJPA.PNG

