started adding support for Spring Data

This commit is contained in:
Mic 2013-01-15 17:31:01 +08:00
parent df5c5ca59d
commit 5432a1932c
11 changed files with 93 additions and 29 deletions

View file

@ -118,6 +118,15 @@
<version>${hibernate.version}</version> <version>${hibernate.version}</version>
</dependency> </dependency>
<!-- **********************************************************************
** SPRING DATA **
********************************************************************** -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
<!-- Servlet --> <!-- Servlet -->
<dependency> <dependency>

View file

@ -43,7 +43,7 @@ import org.springframework.transaction.annotation.Transactional;
* {@link ParameterizedBeanPropertyRowMapper} which provide automatic mapping * {@link ParameterizedBeanPropertyRowMapper} which provide automatic mapping
* between JavaBean properties and JDBC parameters or query results. * between JavaBean properties and JDBC parameters or query results.
* *
* <p>JdbcClinic is a rewrite of the AbstractJdbcClinic which was the base * <p>JdbcClinicImpl is a rewrite of the AbstractJdbcClinic which was the base
* class for JDBC implementations of the Clinic interface for Spring 2.0. * class for JDBC implementations of the Clinic interface for Spring 2.0.
* *
* @author Ken Krebs * @author Ken Krebs
@ -55,7 +55,7 @@ import org.springframework.transaction.annotation.Transactional;
*/ */
@Service @Service
@ManagedResource("petclinic:type=Clinic") @ManagedResource("petclinic:type=Clinic")
public class JdbcClinic implements Clinic, JdbcClinicMBean { public class JdbcClinicImpl implements Clinic, JdbcClinicImplMBean {
private final Logger logger = LoggerFactory.getLogger(getClass()); private final Logger logger = LoggerFactory.getLogger(getClass());

View file

@ -6,14 +6,14 @@ package org.springframework.samples.petclinic.jdbc;
* *
* @author Rob Harrop * @author Rob Harrop
* @author Juergen Hoeller * @author Juergen Hoeller
* @see JdbcClinic * @see JdbcClinicImpl
*/ */
public interface JdbcClinicMBean { public interface JdbcClinicImplMBean {
/** /**
* Refresh the cache of Vets that the Clinic is holding. * Refresh the cache of Vets that the Clinic is holding.
* @see org.springframework.samples.petclinic.Clinic#getVets() * @see org.springframework.samples.petclinic.Clinic#getVets()
* @see JdbcClinic#refreshVetsCache() * @see JdbcClinicImpl#refreshVetsCache()
*/ */
void refreshVetsCache(); void refreshVetsCache();

View file

@ -7,7 +7,7 @@ import org.springframework.samples.petclinic.Pet;
* are only relevant for a JDBC implmentation of the Clinic. * are only relevant for a JDBC implmentation of the Clinic.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @see JdbcClinic * @see JdbcClinicImpl
*/ */
class JdbcPet extends Pet { class JdbcPet extends Pet {

View file

@ -29,7 +29,7 @@ import org.springframework.dao.DataAccessException;
*/ */
@Repository @Repository
@Transactional @Transactional
public class JpaClinic implements Clinic { public class JpaClinicImpl implements Clinic {
@PersistenceContext @PersistenceContext
private EntityManager em; private EntityManager em;

View file

@ -0,0 +1,45 @@
package org.springframework.samples.petclinic.jpa;
import java.util.Collection;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.samples.petclinic.Clinic;
import org.springframework.samples.petclinic.Owner;
import org.springframework.samples.petclinic.Pet;
import org.springframework.samples.petclinic.PetType;
import org.springframework.samples.petclinic.Vet;
import org.springframework.samples.petclinic.Visit;
/**
*
* @author Michael Isvy
* @since 15.1.2013
*/
public interface SpringDataClinic extends Clinic, Repository {
@Query("SELECT vet FROM Vet vet ORDER BY vet.lastName, vet.firstName")
public Collection<Vet> getVets();
@Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name")
public Collection<PetType> getPetTypes();
@Query("SELECT owner FROM Owner owner WHERE owner.lastName LIKE :lastName")
public Collection<Owner> findOwners(String lastName);
public Owner findOwner(int id);
public Pet findPet(int id);
public void storeOwner(Owner owner);
public void storePet(Pet pet);
public void storeVisit(Visit visit);
public void deletePet(int id);
}

View file

@ -3,13 +3,14 @@
Application context definition for PetClinic on JPA. Application context definition for PetClinic on JPA.
--> -->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.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">
<!-- ========================= RESOURCE DEFINITIONS ========================= --> <!-- ========================= RESOURCE DEFINITIONS ========================= -->
@ -88,12 +89,6 @@
--> -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<!--
Will automatically be transactional due to @Transactional.
EntityManager will be auto-injected due to @PersistenceContext.
PersistenceExceptions will be auto-translated due to @Repository.
-->
<bean id="clinic" class="org.springframework.samples.petclinic.jpa.JpaClinic"/>
</beans> </beans>
<beans profile="jdbc"> <beans profile="jdbc">
@ -103,7 +98,22 @@
<!-- PetClinic's central data access object using Spring's SimpleJdbcTemplate --> <!-- PetClinic's central data access object using Spring's SimpleJdbcTemplate -->
<bean id="clinic" class="org.springframework.samples.petclinic.jdbc.JdbcClinic"/> <bean id="clinic" class="org.springframework.samples.petclinic.jdbc.JdbcClinicImpl"/>
</beans>
<beans profile="plain-jpa">
<!--
Will automatically be transactional due to @Transactional.
EntityManager will be auto-injected due to @PersistenceContext.
PersistenceExceptions will be auto-translated due to @Repository.
-->
<bean id="clinic" class="org.springframework.samples.petclinic.jpa.JpaClinicImpl"/>
</beans>
<beans profile="spring-data-jpa">
<jpa:repositories base-package="org.springframework.samples.petclinic.jpa"/>
</beans> </beans>
</beans> </beans>

View file

@ -480,7 +480,7 @@
<p> <p>
The JDBC implementation of the Clinic interface is The JDBC implementation of the Clinic interface is
<span style="font-weight: bold; font-style: italic;">org.springframework.samples.petclinic.jdbc.JdbcClinic</span>, <span style="font-weight: bold; font-style: italic;">org.springframework.samples.petclinic.jdbc.JdbcClinicImpl</span>,
which uses Java 5 language features, which uses Java 5 language features,
<strong>org.springframework.jdbc.core.simple.SimpleJdbcTemplate</strong>, and <strong>org.springframework.jdbc.core.simple.SimpleJdbcTemplate</strong>, and
<strong>org.springframework.jdbc.core.simple.SimpleJdbcInsert</strong>. <strong>org.springframework.jdbc.core.simple.SimpleJdbcInsert</strong>.
@ -513,7 +513,7 @@
<p> <p>
The JPA implementation of the <span style="font-weight: bold;">Clinic</span> The JPA implementation of the <span style="font-weight: bold;">Clinic</span>
interface is interface is
<span style="font-weight: bold; font-style: italic;">org.springframework.samples.petclinic.jpa.JpaClinic</span>, <span style="font-weight: bold; font-style: italic;">org.springframework.samples.petclinic.jpa.JpaClinicImpl</span>,
which is based on native JPA usage combined with Spring's which is based on native JPA usage combined with Spring's
<span style="font-weight: bold;">@Repository</span> and <span style="font-weight: bold;">@Repository</span> and
<span style="font-weight: bold;">@Transactional</span> annotations but <span style="font-weight: bold;">@Transactional</span> annotations but

View file

@ -7,7 +7,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.Clinic; import org.springframework.samples.petclinic.Clinic;
import org.springframework.samples.petclinic.aspects.UsageLogAspect; import org.springframework.samples.petclinic.aspects.UsageLogAspect;
import org.springframework.samples.petclinic.jpa.JpaClinicTests; import org.springframework.samples.petclinic.jpa.JpaClinicImplTests;
import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ -29,7 +29,7 @@ import static junit.framework.Assert.assertFalse;
*/ */
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"}) @ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("jpa") @ActiveProfiles({"jpa","plain-jpa"})
public class UsageLogAspectTests { public class UsageLogAspectTests {
@Autowired @Autowired

View file

@ -9,7 +9,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/** /**
* <p> * <p>
* Integration tests for the {@link JdbcClinic} implementation. * Integration tests for the {@link JdbcClinicImpl} implementation.
* </p> * </p>
* <p> * <p>
* </p> * </p>
@ -21,7 +21,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext @DirtiesContext
@ActiveProfiles("jdbc") @ActiveProfiles("jdbc")
public class JdbcClinicTests extends AbstractClinicTests { public class JdbcClinicImplTests extends AbstractClinicTests {

View file

@ -36,8 +36,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"}) @ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("jpa") @ActiveProfiles({"jpa","plain-jpa"})
public class JpaClinicTests extends AbstractClinicTests { public class JpaClinicImplTests extends AbstractClinicTests {
@PersistenceContext @PersistenceContext
private EntityManager entityManager; private EntityManager entityManager;