How to Configure a JPA Project Using Spring and Hibernate


Solving Interesting Problems by Technical Means

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″

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>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>

View original post 262 more words