spring

Spring MVC, JPA, Hibernate integration

This tutorial show you how to integration Spring mvc, JPA  and Hibernate.  You can check my previous post http://devjav.com/spring-mvc-4-hibernate-4-example/  for further info about spring mvc integration with hibernate.

The technologies we use in this project:

  • Spring MVC 4
  • Eclipse
  • Hibernate 4
  • JPA 2
  • Maven

1. Project structure

2. Maven dependencies

To work with JPA and hibernate we need following dependencies

pom.xml

<properties>
    <spring-version>4.0.3.RELEASE</spring-version>
    <hibernate-version>4.3.4.Final</hibernate-version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- Spring MVC -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring-version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${spring-version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring-version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring-version}</version>
    </dependency>
    <!-- Hibernate -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>${hibernate-version}</version>
    </dependency>
    <!-- Mysql -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.30</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.2.1</version>
      <scope>provided</scope>
    </dependency>
    <!-- DBCP connection pool -->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
    <!-- Taglib -->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>javax.transaction</groupId>
      <artifactId>jta</artifactId>
      <version>1.1</version>
    </dependency>
  </dependencies>

3. Spring configuration files
We use two files for spring config one for service,repository… And the other for web level
ApplicationContext config file: jpaContext.xml
We define:

  • Datasource
  • EntitymanagerFactory
  • Transaction manager

Other services and DAO we using annotation and component scan.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  <context:annotation-config />
  <context:component-scan base-package="com.devjav.service,com.devjav.dao" />
  <tx:annotation-driven transaction-manager="transactionManager" />
  <bean
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
  <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="punit" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="true" />
      </bean>
    </property>

    <property name="jpaPropertyMap">
      <map>
        <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <entry key="hibernate.hbm2ddl.auto" value="update" />
        <entry key="hibernate.format_sql" value="true" />
      </map>
    </property>

  </bean>

  <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
  </bean>

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/tutorial" />
    <property name="username" value="root" />
    <property name="password" value="admin" />
  </bean>
</beans>
DispatcherServlet configuration file: containt bean for web level: View Resolvers, Controllers...

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  <annotation-driven />
  <context:component-scan base-package="com.devjav.controller" />
  <!-- View Resolver -->
  <beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
  </beans:bean>
</beans:beans>

5. Web.xml file
We using context listener to register application context and servlet config for dispatcher context

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>SpringMVC JPA integration</display-name>
        <code>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/jpaContext.xml</param-value>
  </context-param>
       </code>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
        <code>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/servlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
</code>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

6. Data access object
We using @PersistenceContext to inject EntityManager to Repository.

@Repository
public class EmployeeDAO {
  @PersistenceContext
  private EntityManager em;

  public Employee insert(Employee emp) {
    em.persist(emp);
    return emp;
  }

  public Employee update(Employee emp) {
    em.merge(emp);
    return emp;
  }

  public List listAll() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(Employee.class);
    Root emp=cq.from(Employee.class);
    TypedQuery tq = em.createQuery(cq.select(emp));
    return tq.getResultList();
  }

}

7. Source Code
Check my repo springjpa to get source code or download zip file springjpa

2 thoughts on “Spring MVC, JPA, Hibernate integration”

Leave a Reply