Spring Hibernate Integration Example Using Maven

 Posted On  | Yashwant Chavan 

Today we are going to cover simple maven base Spring and Hibernate annotations integration example.Here we are going to configure Hibernate SessionFactory into the spring bean configuration file. As sessionFactory is defined here , we don't need to define "hibernate.cfg.xml" file.

Lets follow the below steps to create sample Spring hibernate application. Or you can directly download the application and import it into your Eclipse as Maven Project.

Useful to whom

This tutorial is useful for beginners and experience developers. It will helps you to learn step by step with the help of attached code.

Tools and Technologies

To execute sample hibernate program we use below technologies

  1. Maven 3.0.4
  2. JDK 1.6
  3. Hibernate 3.2.6.ga
  4. Spring 2.5.6
  5. MySql 5.1

Hibernate Maven Project

Create sample Maven base Project using Eclipse Wizard

Spring Hibernate Integration Example Using Maven

Define Spring and Hibernate Dependencies in pom.xml

Once your Maven base project has been created using Eclipse, Define the below hibernate , spring and other dependencies in pom.xml file.

<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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.technicalkeeda.hibernate</groupId>
 <artifactId>com.technicalkeeda.hibernate</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>HibernateExamples</name>

 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.9</version>
  </dependency>

  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate</artifactId>
   <version>3.2.6.ga</version>
  </dependency>
  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-annotations</artifactId>
   <version>3.3.1.GA</version>
  </dependency>

  <dependency>
   <groupId>dom4j</groupId>
   <artifactId>dom4j</artifactId>
   <version>1.6.1</version>
  </dependency>

  <dependency>
   <groupId>commons-logging</groupId>
   <artifactId>commons-logging</artifactId>
   <version>1.1.1</version>
  </dependency>

  <dependency>
   <groupId>commons-collections</groupId>
   <artifactId>commons-collections</artifactId>
   <version>3.2.1</version>
  </dependency>

  <dependency>
   <groupId>cglib</groupId>
   <artifactId>cglib</artifactId>
   <version>2.2</version>
  </dependency>

  <dependency>
   <groupId>asm</groupId>
   <artifactId>asm</artifactId>
   <version>3.1</version>
  </dependency>

  <dependency>
   <groupId>javax.transaction</groupId>
   <artifactId>jta</artifactId>
   <version>1.1</version>
  </dependency>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring</artifactId>
   <version>2.5.6</version>
                </dependency>
 </dependencies>
 <build>
  <finalName>HibernateExamples</finalName>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>1.6</source>
     <target>1.6</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

Movie.java Model Class

Create Movie model class with the help of hibernate annotations like @Entity, @Table(name = "trn_movies") and @Column(name = "movie_id")

package com.technicalkeeda.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "trn_movies")
public class Movie {

 @Id
 @GeneratedValue
 @Column(name = "movie_id")
 private Long movieId;

 @Column(name = "title")
 private String title;

 @Column(name = "release_year")
 private long releaseYear;

 @Column(name = "director")
 private String director;

 public Long getMovieId() {
  return movieId;
 }

 public void setMovieId(Long movieId) {
  this.movieId = movieId;
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public long getReleaseYear() {
  return releaseYear;
 }

 public void setReleaseYear(long releaseYear) {
  this.releaseYear = releaseYear;
 }

 public String getDirector() {
  return director;
 }

 public void setDirector(String director) {
  this.director = director;
 }

}

MovieDao.java Interface

Define a MovieDao interface with createMovie() method. It is going to save the movie entity into trn_movie table.

package com.technicalkeeda.dao;

import com.technicalkeeda.entities.Movie;

public interface MovieDao {
 public void createMovie(Movie movie);
}

MovieDaoImpl.java Implemention class

MovieDaoImpl is the implementation class. Which implements the createMovie() method.

package com.technicalkeeda.dao.impl;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.technicalkeeda.dao.MovieDao;
import com.technicalkeeda.entities.Movie;

public class MovieDaoImpl implements MovieDao {

 private HibernateTemplate hibernateTemplate;

 public void setSessionFactory(SessionFactory sessionFactory) {
  this.hibernateTemplate = new HibernateTemplate(sessionFactory);
 }

 @Override
 public void createMovie(Movie movie) {
  hibernateTemplate.saveOrUpdate(movie);
 }

}

spring-configuration.xml Spring Hibernate Inegration Configuration File

As you know that in spring application , there are lot of configuration we need to do that. Like spring datasource configuration, spring bean configuration and hibernate session factory configuration.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
 
 <!--  This is Database Configuration -->
 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
  <property name="url"><value>jdbc:mysql://localhost:3306/technicalkeeda</value></property>
  <property name="username"><value>root</value></property>
  <property name="password"><value></value></property>
 </bean> 
 
 <bean id="movieDao" class="com.technicalkeeda.dao.impl.MovieDaoImpl"> 
  <property name="sessionFactory" ref="hibernateSessionFactory"/>
 </bean>
 
 <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="annotatedClasses">
   <list><value>com.technicalkeeda.entities.Movie</value></list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">create</prop>
   </props>
  </property>
 </bean>   

</beans>

Test Class (App.java)

Build your maven project and execute the App.java class, It is going to create trn_movie table in the database and insert two records of movies ("Life of Pi" and "Dabangg 2")

package com.technicalkeeda.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.technicalkeeda.dao.MovieDao;
import com.technicalkeeda.entities.Movie;

public class App {
 public static void main(String[] args) {
  ApplicationContext appContext = new ClassPathXmlApplicationContext(
    "config/spring-configuration.xml");

  Movie lifeOfPi = new Movie();
  lifeOfPi.setReleaseYear(2012);
  lifeOfPi.setDirector("Ang Lee");
  lifeOfPi.setTitle("Life of Pi");

  Movie dabangg2 = new Movie();
  dabangg2.setReleaseYear(2012);
  dabangg2.setDirector("Arbaaz Khan");
  dabangg2.setTitle("Dabangg 2");

  MovieDao movieDao = (MovieDao) appContext.getBean("movieDao");
  movieDao.createMovie(lifeOfPi);
  movieDao.createMovie(dabangg2);

 }
}

trn_movie Table

Two records are inserted into the database table

Hibernate Spring Integration Example

Output

When you execute the App.java , output will look like this.

Hibernate: insert into trn_movies (director, release_year, title) values (?, ?, ?)
Hibernate: insert into trn_movies (director, release_year, title) values (?, ?, ?)

Step #7 Finish

Hope this Tutorial is useful to you, Please fill free to provide your comments if there is any problem. Thanks


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0335 seconds.