Getting Started with Hibernate 3 and NetBeans 6.5 Beta Part 1
Update (17/9/08): The next article in this series can be found here. Update (5/10/08): Third part, covering Hibernate relationships, here.
NetBeans 6.5 ships with Hibernate libraries and generators built in, making it easier than ever to get started with Hibernate. If the bugs in 6.5 bug you, you can always generate what you need in 6.5 and then open the project up in 6.1. However, there if you are going to do that, prepare to manually add in some libraries yourself.
Why you should be using Hibernate
All savvy web developers strive to maintain clean separation of codes respective to their domains in the MVC pattern. Hibernate helps us out in the data layer, or model code. Hibernate is an ORM that maps rows in the database to objects in an OO application. It saves us from having to write SQL commands and transferring information to objects and vice versa.
You should still have your entity and data access classes, although your DAO should be significantly reduced in size minus the SQL to object code that you have to keep writing. Hibernate is flexible enough to cater to just about any kind of database schema so instead of rolling your own ORM, you should consider the use of Hibernate. The best code is those that you didn’t have to write.
Getting Started
You will need to download NetBeans 6.5 and this SQL script that I’ve created for my individual assignment. Once you’ve installed the IDE, fire it up and check for any updates and install them. Use the SQL script to create the tables in your MySQL database.
Let’s create a Java Web Application, and remember to select the Hibernate framework to be used in this project. You will be able to set a new JDBC connection to the database you should have created by now.


Looking at your project directory, you will notice a hibernate.cfg.xml file lurking in your default package. Opening this up in XML view will show your Hibernate configuration information. Do remember to set the database username & password if you haven’t already.
Hibernate provides sessions by which your code can interact with the database. To make things simple, we will be letting Hibernate manage our sessions. Add this line to your Hibernate configuration file.
<hibernate-configuration>
<session-factory>
...
<property name="current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
Since we’re using MySQL as our database, we’ll have to add the library to the project, like so.


We’ll need one more piece of the puzzle, so generate the HibernateUtil.java file like so in your data package.

Generating POJOs
POJOs, are your entity classes. You’ll be glad to know that you won’t have to write any of it yourselves. Simply right click on your project in your Project tab and select New->Hibernate Mapping Files and POJOs from Database.

Follow the instructions and you’ll be able to select which tables to map. Just select them all in this tutorial.

I like to put my entity classes in a package by themselves as such. Hint: you should do the same.

About these files

Now, you’ll see that you have a hibernate.reveng.xml file; it’s not really important to us and I don’t know what it does really (like a lot of other things in Java). More importantly, look at the hibernate.cfg.xml file. You’ll see that they’ve mapped some resources that can be found in your entities package folder.
Navigate to these files and you will find both your entity classes and Hibernate mapping files. Open up the User.java and User.hbm.xml files and take a minute to see how database fields are mapped to java attributes.
Inserting a record into database
Yes, you haven’t written a line of code, but you can do this now. Go to your User.java class where you will create a main method. We’ll be adding a simplified test case here. You can just copy and paste this code at the end of the User class.
public static void main(String [] args) {
User user = new User("Jaryl", "123");
SessionFactory factory = HibernateUtil.getSessionFactory();
Session session = factory.getCurrentSession();
try {
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
} catch (Throwable e) {
if (session.getTransaction().isActive()) {
session.getTransaction().rollback();
}
System.out.println(e.getMessage());
}
}
Build the entire project but do not run it. Instead, run the file individually by pressing Shift-F6 or through the context menu. In case you’re wondering, we build the entire project because Hibernate will attempt to load the other entities as well, and will fail if the .class files of other entities were not existent.

There shouldn’t be any errors and you should end up with a new User entry in your database. The test case ends with you manually checking if the record can be found in the database. I said it was simplified right?
Well, that’s all for now, so look out for part 2. In the meantime, learn more about Hibernate from these excellent Hibernate tutorials. Some of the examples did not work out of the box for me, so you have to wait for part 2, where I probably be show you how to do CRUD operations and maybe cover some relationship mapping.