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

3 thoughts on “Getting started with Hibernate 4 and Maven

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s