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

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

Getting started with Richfaces 4 and Maven

I tried to create a basic Richfaces project using the document : http://docs.jboss.org/richfaces/latest_4_X/Developer_Guide/en-US/pdf/Developer_Guide.pdf but ran into trouble with the following line:

mvn archetype:generate -DarchetypeGroupId=org.richfaces.archetypes -DarchetypeArtifactId=richfaces-archetype-simpleapp -DarchetypeVersion=4.0.0-SNAPSHOT -DgroupId=org.docs.richfaces -DartifactId=new_project.


As of Dec 2011, the issue is that my installation of Maven (ver. 3.0.3) would fail since it can’t find the required archetype. ¬†The latest archetype version being 4.1.0.Final, this ¬†works only when the ‘archetypeVersion’ in the above command line is changed to ‘4.1.0.Final’.

Hope this helps someone who’s just getting started with Richfaces and Maven.