How to Configure a JPA Project Using Spring and Hibernate

Introduction

In most Java projects that involve a database, a ORM(Object Relational Mapping) layer is needed. This can be done by hand via JDBC. Spring does make the job easier however with a JdbcTemplate. The other way is to use one of the libraries that is popular is Hibernate. Another standard that is getting popular is JPA (Java Persistence API). The advantage of JPA is that it standardizes the API between ORM libraries and the DAO (Data Access Object) layer. This makes it easier to swap out different ORM libraries as requirements change. In this post I am going to show how to configure Hibernate with Spring using the JPA standard on a Java SE application.

Example

Maven pom.xml

Here is the pom.xml file used to pull the dependencies

<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243;

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;

xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&#8221; >

<modelVersion>4.0.0</modelVersion>

<groupId>org.mathison</groupId>

<artifactId>ForTheLittleGuy</artifactId>

<version>1.0-SNAPSHOT</version>

<packaging>jar</packaging>

<name>ForTheLittleGuy</name>

<url>http://maven.apache.org</url&gt;

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>2.3.2</version>

<configuration>

<source>1.7</source>

<target>1.7</target>

</configuration>

</plugin>

</plugins>

</build>

<repositories>

<repository>

<url>http://download.eclipse.org/rt/eclipselink/maven.repo/</url&gt;

<id>jpa20-persistence</id>

<layout>default</layout>

<name>Repository for library Persistence (JPA 2.1)</name>

</repository>

</repositories>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.11</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>3.2.3.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>3.2.3.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>3.2.3.RELEASE</version>

</dependency>

<dependency>

<groupId>org.eclipse.persistence</groupId>

<artifactId>javax.persistence</artifactId>

<version>2.1.0-SNAPSHOT</version>

</dependency>

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-entitymanager</artifactId>

<version>4.2.3.Final</version>

</dependency>

<dependency>

<groupId>org.hibernate.javax.persistence</groupId>

<artifactId>hibernate-jpa-2.0-api</artifactId>

<version>1.0.1.Final</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>3.2.3.RELEASE</version>

</dependency>

<dependency>

<groupId>org.apache.derby</groupId>

<artifactId>derby</artifactId>

<version>10.10.1.1</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-orm</artifactId>

<version>3.2.3.RELEASE</version>

</dependency>

</dependencies>

</project>

Spring Files

main-config.xml

This is the main configuration file where the Spring framework configures Hibernate for its ORM layer. The context entries allow for annotations to be used to inject dependencies into classes.

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”http://www.springframework.org/schema/beans&#8221;

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;

xmlns:context=”http://www.springframework.org/schema/context&#8221;

xmlns:tx=”http://www.springframework.org/schema/tx&#8221;

xmlns:jdbc=”http://www.springframework.org/schema/jdbc&#8221;

xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd

“>

<jdbc:embedded-database id=”dataSource” type=”DERBY”>

<jdbc:script location=”contact.sql”/>

<jdbc:script location=”InsertContacts.sql” />

</jdbc:embedded-database>

<tx:annotation-driven transaction-manager=”transactionManager”/>

<bean id=”transactionManager” class=”org.springframework.orm.jpa.JpaTransactionManager”>

<property name=”entityManagerFactory” ref=”emf”/>

<bean id=”emf” class=”org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean”>

<property name=”dataSource” ref=”dataSource”/>

<property name=”jpaVendorAdapter”>

<bean class=”org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter”/>

</property>

<property name=”packagesToScan” value=”org.mathison.fortlg.data”/>

<property name=”jpaProperties”>

<props>

<prop key=”hibernate.dialect”>

org.hibernate.dialect.DerbyTenSevenDialect

</prop>

<prop key=”hibernate.max_fetch_depth”>3</prop>

<prop key=”hibernate.jdbc.fetch_size”>50</prop>

<prop key=”hibernate.jdbc.batch_size”>10</prop>

<prop key=”hibernate.show_sql”>true</prop>

</props>

</property>

<context:annotation-config/>

<context:component-scan base-package=”org.mathison.fortlg”/>

</beans>

app-config.xml

This is a second configuration file that defines two profiles. Each profile defines a different implementation of a DAO.

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”http://www.springframework.org/schema/beans&#8221;

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;

xmlns:p=”http://www.springframework.org/schema/p&#8221;

xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd

“>

<beans profile=”criteria”>

<bean id=”contactDao” class=”org.mathison.fortlg.dao.ContactDaoCriteriaImpl”/>

</beans>

<beans profile=”jpql”>

<bean id=”contactDao” class=”org.mathison.fortlg.dao.ContactDaoJpqlImpl”/>

</beans>

</beans>

Conclusion

This is an example of how to define Hibernate as the JPA provider with the Spring Framework. These files plus the entire source code of the maven project can be found at http://fortlg.googlecode.com/svn/trunk/ForTheLittleGuy.

1 thought on “How to Configure a JPA Project Using Spring and Hibernate

Comments are closed.