SQL Zone is brought to you in partnership with:

I'm Constantin DRABO from Ouagadougou, Burkina Faso. I'm fervent advocate of Java technologies and Linux based plateforms. So, I contribute to several open source communities such as NetBeans , Java, Fedora and Google. Drabo has posted 12 posts at DZone. You can read more from them at their website. View Full User Profile

How to Use Hibernate in a NetBeans Platform Application

08.11.2010
| 43268 views |
  • submit to reddit

In this tutorial I show you how to combine a NetBeans Platform application with Hibernate. For this simple getting started example, I use the basic Hibernate features.

This tutorial is based on the Patient Administration application built with Geertjan during the NetBeans Platform Certified training session in Burkina Faso. The application is very basic. It enables the insertion and deletion of a patient, while it also enables the retrieval of the list of patients for display in an explorer window:

Create a Database

First of all you need to create a database “mydatabase as shown in the picture below. As a sample database, there is no need for any password for the administrator:


Secondly, create a table named "patient", using the script below:



Create a NetBeans Platform Application

As stated above, the tutorial is based on an application built during a NetBeans Platform training session. There are three main modules: ModelPatient, PatientView, and PatientWindows. The fourth, hibernate, is a wrapper library module.


ModelPatient contains the POJO class, mapping files, and Hibernate configuration file:

The POJO class is here named "Patient.java". You can look here at a listing of the source code:

You must create a mapping file, as follows:

In order to allow mapping file to access the Patient entity, you must specify the absolute path of the Patient class in the mapping file above, as follows:

<class name="org.netbeans.modelpatient.Patient" table="patient">

An important file to be created is the Hibernate configuration file. I named it hibernate.cfg.xml:

In order to allow the Hibernate configuration file to access the mapping resource, you must specify the absolute path of the mapping file. In my example, above, the mapping file is located in package org.netbeans.patientmodel. Then in the configuration file I have the following:

<mapping resource="org/netbeans/modelpatient/Patient.hbm.xml"/>

HibernateUtil.java is a standard Java class that lets you perform persistence operations. A session factory is important for Hibernate. It implements a design pattern that ensures that only one instance of the session is used per thread. You should only get your Hibernate session from this factory:

This part of the code is extremely important. When the Hibernate configuration file is not on the CLASSPATH you must specify explicitly its path, as shown above:

    static {
URL myurl = Thread.currentThread().getContextClassLoader().getResource("org/netbeans/modelpatient/hibernate.cfg.xml");
sessionFactory = new Configuration().configure(myurl).buildSessionFactory();
}

To configure Log4J, create a file named log4j.properties in the root directory and insert the following:

The wrapper library module "hibernate" contains the Hibernate and MySQL libraries:

PatientWindows provides a window with two buttons, Save and Cancel:

Here is the source code for the Save button:

Under the Cancel button, add this code to quit the application:

LifecycleManager.getDefault().exit();
The APIs necessary for this module are:

 

In the PatientView module, the modification you need to make is to dynamically set the list of patients from the database to the node. Here the "createKeys" method in the node is used as follows:


 

The APIs needed in this module are as follows:

 

Published at DZone with permission of its author, Drabo Constantin.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)