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