Getting started with Hibernate 4 and Maven

For all those folks who are trying to find out how to use Hibernate 4 and Maven here goes:

Check out the attached source code. The main changes are in the pom.xml and in the HibernateUtil class. As you might already know, the earlier Configuration.buildSessionFactory is deprecated. So, one must use the  following to configure a SessionFactory object:

private static SessionFactory SESSIONFACTORY = null;
private static ServiceRegistry SERVICEREGISTRY = null;
try{
Configuration configuration = new Configuration();
configuration.configure(“hibernate.cfg.xml”);SERVICEREGISTRY = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
SESSIONFACTORY = configuration.buildSessionFactory(SERVICEREGISTRY);}catch (Exception e) {
System.err.println(“Initial SessionFactory creation failed.” + e);
throw new ExceptionInInitializerError(e);
}

Other than that, its pretty straight forward if you have the correct Maven installation and the pom.xml 🙂

Here’s the code. Just download and build using maven. Its an Eclipse project – so you can Import As -> Existing Maven Project provided you have M2E plugin installed: (Use GitHub to access the code)

https://github.com/trishulpani/Hibernate4

Advertisements

SpringMVC-Hibernate-Maven-Glassfish-Netbeans sample project

This post is a slightly modified version of http://viralpatel.net/blogs/2010/11/spring3-mvc-hibernate-maven-tutorial-eclipse-example.html

(For explanations, see the original blog) that uses the following tech stack:

Spring MVC (3.0.2.RELEASE), Hibernate (3.6.7.Final), JEE 6.0, JPA 2.0 (2.0-cr-1) and Spring (3.0.2.RELEASE).

Functionally both are same. This just works with the said tech stack as I have made the correct entries to work with Maven2.

The main modifications boiled down to the Project’s pom.xml and the fact that I had to add @Transactional to the service methods – otherwise I was getting runtime exception that said there was no active transaction.

Download the project and MySQL script files to get started. The application, once built (import to Netbeans and Build) – can be deployed to Glassfish.:

https://docs.google.com/open?id=0B6uhVH07AfMPZmZkYzA0MmItZjAzYS00MjliLTgxMzQtYmY3MTRiOGY1Njlh

MySQL script is here: https://docs.google.com/open?id=0B6uhVH07AfMPMGQ2MmQzYjYtOTMyNi00MmVjLWFkZDQtN2Q1MDdjNTJhMWFm