Hibernate Tutorials : Setting up the environment for Hibernate

Setting up the Environment for Hibernate

For practicing Hibernate with some examples, we need to first setup our local environment. Hibernate don’t need any application server in order to perform transaction.

Step 1 : Understand Required Jars

First thing, we will need the required jar of Hibernate framework. It is not necessary to include all of the jars just to run a simple example. Following is the list of jars for simple hibernate example.

hibernate-core :  Hibernate core is the basic building block of the Hibernate framework which is required to run the application.

mysql-connector-java : Since we are connecting with MySql database then we need connector which helps to communicate with database server.

commons-dbcp : Commons DBCP is required jar if you want to add connection pooling functionality.

Step 2 : Setup Maven in your location system

Maven is dependency management tool so you don’t need to manually add the required libraries. Maven takes the responsibility and manage all dependencies automatically. Install and configure Maven on your system, found this link helpful Configure Maven.

Step 3 : Create Maven web app project

You can also create simple maven project, you don’t need to create web application project. But we are creating web app project for further examples. Please follow these steps to create maven project.

Go to File -> New -> Maven Project



Then click Next button



Select maven-archetype-webapp as below



Provide details for group id, artifact id and package and click on finish button to finish the setup.



 

That’s it, Maven project is created.

Step 4 : Configure dependencies in pom.xml

Maven uses pom.xml configuration file to manage dependencies. You can provide dependency details in pom.xml as below.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.hibernatetest</groupId>
 <artifactId>HibernateTest</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>HibernateTest Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <properties>
 <hibernate.version>4.3.5.Final</hibernate.version>
 <slf4j.version>1.7.6</slf4j.version>
 <dbcp.version>1.4</dbcp.version>
 <mysql.connector.version>5.1.30</mysql.connector.version>
 </properties>

 <dependencies>


 <!-- database pool -->
 <dependency>
 <groupId>commons-dbcp</groupId>
 <artifactId>commons-dbcp</artifactId>
 <version>${dbcp.version}</version>
 </dependency>

 <!-- Hibernate ORM -->
 <dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-core</artifactId>
 <version>${hibernate.version}</version>
 </dependency>

 <!-- MySql Driver -->
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>${mysql.connector.version}</version>
 </dependency>

 <!-- logging, slf4j -->
 <dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-api</artifactId>
 <version>${slf4j.version}</version>
 </dependency>

 <dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>jcl-over-slf4j</artifactId>
 <version>${slf4j.version}</version>
 </dependency>


 </dependencies>
 <build>
 <finalName>HibernateTest</finalName>
 </build>
</project>
Now you are ready to run examples on hibernate.

Leave a Reply

Your email address will not be published. Required fields are marked *