06
Jun
10

Starting a Simple Java Persistence (JPA) Project Using Spring


This page describes the process of converting an existing JPA project to use the Spring Framework. We will pick up where we left off in my previous post.

Requirements

Step 1

Modify the pom.xml file and add the dependency for the spring framework.

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring</artifactId>
    <version>2.5.6</version>
</dependency>

Save and close and regenerate and refresh the project in eclipse

mvn eclipse:clean eclipse:eclipse

Step 2

Create the Data Manager class. This class is responsible for dealing with the entityManager.

src/main/java/com/test/AuctionDataManagerJpaImpl.java

package com.test;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

@Repository("auctionDataManager")
public class AuctionDataManagerJpaImpl implements AuctionDataManager {
	private EntityManager entityManager;

	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	/* (non-Javadoc)
	 * @see com.test.AuctionDataManager#loadBidsByItemId(java.lang.Long)
	 */
	public List<Bid> loadBidsByItemId(Long itemId) {
		return entityManager.createQuery("from Bid b where b.item.id = :itemId")
			.setParameter("itemId", itemId).getResultList();
	}
}

The following is the interface for the above class.

src/main/java/com/test/AuctionDataManager.java

package com.test;

import java.util.List;

public interface AuctionDataManager {
	public abstract List<Bid> loadBidsByItemId(Long itemId);
}

Step 3

Add the following 2 lines into the spring configuration.

src/main/resources/applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 
http://www.springframework.org/schema/context
 
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
    <context:annotation-config/>
 
    <context:component-scan base-package="com.test"/>

	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" />
	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 
 
</beans>

Add the following method into the test case.

src/test/java/com/test/AppTest.java

// put the following up top
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

// insert the following at the bottom
	/**
	 * This method is responsible for getting the results using
	 * spring Entity Manager.
	 *
	 * @throws Exception
	 */
	public void testSpringJPA() throws Exception {
		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
		AuctionDataManager auctionDataManager = (AuctionDataManager) context.getBean("auctionDataManager");

		System.out.println(auctionDataManager.loadBidsByItemId(Long.valueOf(1)));

	}

Step 4

Run the test by running it in eclipse or type on the command line:

mvn test

Observe the output. It should print the results of the query you did using JPA.

Advertisements

4 Responses to “Starting a Simple Java Persistence (JPA) Project Using Spring”


  1. 1 Long
    July 3, 2010 at 1:18 pm

    auctionDataManager used in testSpringJPA is not defined in applicationContext.xml
    and entityManager in AuctionDataManagerJpaImpl is not defined in applicationContext.xml. instead the factory is there.

    could you show me the entire applicationContext.xml?

    thanks

    Long


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 77 other followers

June 2010
S M T W T F S
« May   Aug »
 12345
6789101112
13141516171819
20212223242526
27282930  

Blog Stats

  • 847,163 hits

%d bloggers like this: