Monday, February 20, 2012

Hibernate - Setup

We are going to update our UserDAO to use Hibernate.

First, drop the 'users' table from your confuciusDB. Hibernate has its own rules for mapping classes to database schema.

The command for this in MySQL Command Line Client is:

 mysql> drop table users;  

Next, update your ivy.xml to get the hibernate-core.jar, like this:

(I have to add all the dependencies of hibernate-core.jar explicitly because of the way my internal maven repository is setup - you might not need to if you are pointing to Maven central repository)
 <ivy-module version="2.0">  
<info organisation="org.confucius" module="helloworld"/>
<dependencies>
<dependency org="javassist" name="javassist" rev="3.12.1.GA"/>
<dependency org="mysql" name="mysql-connector-java" rev="5.1.18"/>
<dependency org="javax.persistence" name="persistence-api" rev="1.0.2"/>
<dependency org="jboss" name="jboss-j2ee" rev="4.2.2.GA"/>
<dependency org="org.jboss.logging" name="jboss-logging" rev="3.1.0.GA"/>
<dependency org="org.hibernate.common" name="hibernate-commons-annotations" rev="4.0.1.Final"/>
<dependency org="org.hibernate" name="hibernate-annotations" rev="3.5.6-Final"/>
<dependency org="dom4j" name="dom4j" rev="1.6.1"/>
<dependency org="org.hibernate" name="hibernate-core" rev="4.0.1.Final"/>
<dependency org="org.springframework" name="spring" rev="2.5.6"/>
<dependency org="struts" name="struts" rev="1.2.9"/>
<dependency org="org.apache.struts" name="struts-taglib" rev="1.3.10"/>
<dependency org="commons-collections" name="commons-collections" rev="20040616"/>
<dependency org="commons-digester" name="commons-digester" rev="2.1"/>
<dependency org="commons-beanutils" name="commons-beanutils" rev="20030211.134440"/>
<dependency org="commons-logging" name="commons-logging" rev="1.1.1"/>
<dependency org="org.apache.httpcomponents" name="httpcore" rev="4.2-alpha2"/>
<dependency org="org.apache.httpcomponents" name="httpclient" rev="4.2-alpha1"/>
<dependency org="org.apache.commons" name="commons-exec" rev="1.1"/>
<dependency org="com.google.guava" name="guava" rev="r09"/>
<dependency org="org.seleniumhq.selenium" name="selenium-api" rev="2.17.0"/>
<dependency org="org.seleniumhq.selenium" name="selenium-remote-driver" rev="2.17.0"/>
<dependency org="org.seleniumhq.selenium" name="selenium-firefox-driver" rev="2.17.0"/>
<dependency org="org.seleniumhq.selenium" name="selenium-java" rev="2.16.1"/>
<dependency org="junit" name="junit" rev="4.10"/>
<dependency org="org.json" name="json" rev="20090211"/>
<dependency org="javax.servlet" name="servlet-api" rev="2.5"/>
<dependency org="javax.servlet" name="jsp-api" rev="2.0"/>
<dependency org="jstl" name="jstl" rev="1.2"/>
<dependency org="log4j" name="log4j" rev="1.2.16"/>
</dependencies>
</ivy-module>


Run your Ant 'resolve' target to download the hibernate-core.jar to your /lib directory.
It might also download additional jars on which this jar depends.

Update the Eclipse Classpath (Project-->Properties-->Java Build Path-->Libraries-->Add Jars) to include the hibernate-core.jar, and any other jars on which this jar depends.

Next, create a file hibernate.cfg.xml in your /classes directory (same level as /src), like this:

 <?xml version="1.0" encoding="utf-8"?>  
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/confuciusDB</property>
<property name="hibernate.connection.username">confucius</property>
<property name="hibernate.connection.password">changeit</property>

</session-factory>
</hibernate-configuration>


This tells Hibernate how to connect to your database, including username and password.

The dialect property tells Hibernate we are using a MySQL database.

If, later, we decide to use Oracle or any of the other 16 or so databases supported by Hibernate, we need to only change this one property!


Add your /classes folder to your Eclipse Classpath. (Project-->Properties-->Java Build Path-->Source-->Add Folders).
We need to do this because we will be running the Hibernate example directly from Eclipse.


Finally, in your /src/org/confucius, create a class HibernateUtil.java which can load the Hibernate configuration, like this:

 package org.confucius;  

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
return configuration.buildSessionFactory(serviceRegistry);
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

public static void shutdownSessionFactory() {
sessionFactory.close();
}
}



This class loads the Hibernate configuration.

By default the hibernate.cfg.xml file is loaded to create a SessionFactory.

If you like, you can specify the exact name of the file in the configuration.configure(), something like configuration.configure("foo.cfg.xml").

This is especially useful if you want to connect to multiple databases. You can create multiple .cfg.xml files, one for each database. Then create multiple SessionFactorys - one for each .cfg.xml.

No comments: