added a Spring Data implementation

- there are now 3 data access implementations (jdbc, jpa and
spring-data-jpa)
- added corresponding bean profiles
- JUnit tests are now successful
This commit is contained in:
Mic 2013-01-18 16:56:01 +08:00
parent c9c8c4e085
commit 97aba3f4e6
50 changed files with 368 additions and 426 deletions

View file

@ -2,7 +2,6 @@ package org.springframework.samples.petclinic;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotEmpty;

View file

@ -18,6 +18,7 @@ package org.springframework.samples.petclinic;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

View file

@ -1,31 +0,0 @@
package org.springframework.samples.petclinic.aspects;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Aspect to illustrate Spring-driven load-time weaving.
*
* @author Ramnivas Laddad
* @since 2.5
*/
@Aspect
public abstract class AbstractTraceAspect {
private static final Logger logger = LoggerFactory.getLogger(AbstractTraceAspect.class);
@Pointcut
public abstract void traced();
@Before("traced()")
public void trace(JoinPoint.StaticPart jpsp) {
if (logger.isTraceEnabled()) {
logger.trace("Entering " + jpsp.getSignature().toLongString());
}
}
}

View file

@ -2,11 +2,8 @@ package org.springframework.samples.petclinic.aspects;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.util.StopWatch;
/**
@ -17,8 +14,8 @@ import org.springframework.util.StopWatch;
* @author Juergen Hoeller
* @since 2.5
*/
@ManagedResource("petclinic:type=CallMonitor")
@Aspect
//@ManagedResource("petclinic:type=CallMonitor")
//@Aspect
public class CallMonitoringAspect {
private boolean isEnabled = true;

View file

@ -30,7 +30,7 @@ public class UsageLogAspect {
this.namesRequested = new ArrayList<String>(historySize);
}
@Before("execution(* *.findOwners(String)) && args(name)")
@Before("execution(* *.find*(String)) && args(name)")
public synchronized void logNameRequest(String name) {
// Not the most efficient implementation,
// but we're aiming to illustrate the power of

View file

@ -1,14 +1,11 @@
package org.springframework.samples.petclinic.repository;
import java.util.Collection;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.samples.petclinic.BaseEntity;
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;
/**
* The high-level PetClinic business interface.
@ -26,7 +23,7 @@ public interface PetRepository {
* Retrieve all <code>PetType</code>s from the data store.
* @return a <code>Collection</code> of <code>PetType</code>s
*/
Collection<PetType> getPetTypes() throws DataAccessException;
List<PetType> findPetTypes() throws DataAccessException;
/**
* Retrieve a <code>Pet</code> from the data store by id.
@ -41,11 +38,6 @@ public interface PetRepository {
* @param pet the <code>Pet</code> to save
* @see BaseEntity#isNew
*/
void storePet(Pet pet) throws DataAccessException;
/**
* Deletes a <code>Pet</code> from the data store.
*/
void deletePet(int id) throws DataAccessException;
void save(Pet pet) throws DataAccessException;
}

View file

@ -21,7 +21,7 @@ public interface VetRepository {
* Retrieve all <code>Vet</code>s from the data store.
* @return a <code>Collection</code> of <code>Vet</code>s
*/
Collection<Vet> getVets() throws DataAccessException;
Collection<Vet> findAll() throws DataAccessException;
}

View file

@ -23,7 +23,7 @@ public interface VisitRepository {
* @param visit the <code>Visit</code> to save
* @see BaseEntity#isNew
*/
void storeVisit(Visit visit) throws DataAccessException;
void save(Visit visit) throws DataAccessException;
List<Visit> findByPetId(Integer petId);

View file

@ -1,20 +0,0 @@
package org.springframework.samples.petclinic.repository.jdbc;
/**
* Interface that defines a cache refresh operation.
* To be exposed for management via JMX.
*
* @author Rob Harrop
* @author Juergen Hoeller
* @see JdbcClinicImpl
*/
public interface JdbcClinicImplMBean {
/**
* Refresh the cache of Vets that the ClinicService is holding.
* @see org.springframework.samples.petclinic.service.ClinicService#getVets()
* @see JdbcClinicImpl#refreshVetsCache()
*/
void refreshVetsCache();
}

View file

@ -5,8 +5,6 @@ import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
@ -15,7 +13,6 @@ import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.samples.petclinic.Owner;
import org.springframework.samples.petclinic.Pet;
@ -24,22 +21,13 @@ import org.springframework.samples.petclinic.Visit;
import org.springframework.samples.petclinic.repository.OwnerRepository;
import org.springframework.samples.petclinic.repository.PetRepository;
import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.samples.petclinic.util.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* A simple JDBC-based implementation of the {@link ClinicService} interface.
* A simple JDBC-based implementation of the {@link OwnerRepository} interface.
*
* <p>This class uses Java 5 language features and the {@link SimpleJdbcTemplate}
* plus {@link SimpleJdbcInsert}. It also takes advantage of classes like
* {@link BeanPropertySqlParameterSource} and
* {@link ParameterizedBeanPropertyRowMapper} which provide automatic mapping
* between JavaBean properties and JDBC parameters or query results.
*
* <p>JdbcClinicImpl is a rewrite of the AbstractJdbcClinic which was the base
* class for JDBC implementations of the ClinicService interface for Spring 2.0.
*
* @author Ken Krebs
* @author Juergen Hoeller

View file

@ -1,54 +1,29 @@
package org.springframework.samples.petclinic.repository.jdbc;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.samples.petclinic.Owner;
import org.springframework.samples.petclinic.Pet;
import org.springframework.samples.petclinic.PetType;
import org.springframework.samples.petclinic.Specialty;
import org.springframework.samples.petclinic.Vet;
import org.springframework.samples.petclinic.Visit;
import org.springframework.samples.petclinic.repository.OwnerRepository;
import org.springframework.samples.petclinic.repository.PetRepository;
import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.samples.petclinic.util.EntityUtils;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* A simple JDBC-based implementation of the {@link ClinicService} interface.
*
* <p>This class uses Java 5 language features and the {@link SimpleJdbcTemplate}
* plus {@link SimpleJdbcInsert}. It also takes advantage of classes like
* {@link BeanPropertySqlParameterSource} and
* {@link ParameterizedBeanPropertyRowMapper} which provide automatic mapping
* between JavaBean properties and JDBC parameters or query results.
*
* <p>JdbcClinicImpl is a rewrite of the AbstractJdbcClinic which was the base
* class for JDBC implementations of the ClinicService interface for Spring 2.0.
*
* @author Ken Krebs
* @author Juergen Hoeller
@ -71,9 +46,6 @@ public class JdbcPetRepositoryImpl implements PetRepository {
@Autowired
private VisitRepository visitRepository;
private final List<Vet> vets = new ArrayList<Vet>();
@Autowired
public void init(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
@ -84,14 +56,12 @@ public class JdbcPetRepositoryImpl implements PetRepository {
.usingGeneratedKeyColumns("id");
}
@Transactional(readOnly = true)
public Collection<PetType> getPetTypes() throws DataAccessException {
public List<PetType> findPetTypes() throws DataAccessException {
return this.jdbcTemplate.query(
"SELECT id, name FROM types ORDER BY name",
ParameterizedBeanPropertyRowMapper.newInstance(PetType.class));
}
@Transactional(readOnly = true)
public Pet findById(int id) throws DataAccessException {
JdbcPet pet;
try {
@ -105,7 +75,7 @@ public class JdbcPetRepositoryImpl implements PetRepository {
}
Owner owner = this.ownerRepository.findById(pet.getOwnerId());
owner.addPet(pet);
pet.setType(EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId()));
pet.setType(EntityUtils.getById(findPetTypes(), PetType.class, pet.getTypeId()));
List<Visit> visits = this.visitRepository.findByPetId(pet.getId());
for (Visit visit : visits) {
@ -114,8 +84,7 @@ public class JdbcPetRepositoryImpl implements PetRepository {
return pet;
}
@Transactional
public void storePet(Pet pet) throws DataAccessException {
public void save(Pet pet) throws DataAccessException {
if (pet.isNew()) {
Number newKey = this.insertPet.executeAndReturnKey(
createPetParameterSource(pet));
@ -142,19 +111,4 @@ public class JdbcPetRepositoryImpl implements PetRepository {
.addValue("owner_id", pet.getOwner().getId());
}
@Override
public void deletePet(int id) throws DataAccessException {
// TODO Auto-generated method stub
}
/**
* Loads the {@link Pet} and {@link Visit} data for the supplied
* {@link Owner}.
*/
}

View file

@ -6,46 +6,22 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.samples.petclinic.Owner;
import org.springframework.samples.petclinic.Pet;
import org.springframework.samples.petclinic.PetType;
import org.springframework.samples.petclinic.Specialty;
import org.springframework.samples.petclinic.Vet;
import org.springframework.samples.petclinic.Visit;
import org.springframework.samples.petclinic.repository.VetRepository;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.samples.petclinic.util.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* A simple JDBC-based implementation of the {@link ClinicService} interface.
*
* <p>This class uses Java 5 language features and the {@link SimpleJdbcTemplate}
* plus {@link SimpleJdbcInsert}. It also takes advantage of classes like
* {@link BeanPropertySqlParameterSource} and
* {@link ParameterizedBeanPropertyRowMapper} which provide automatic mapping
* between JavaBean properties and JDBC parameters or query results.
*
* <p>JdbcClinicImpl is a rewrite of the AbstractJdbcClinic which was the base
* class for JDBC implementations of the ClinicService interface for Spring 2.0.
*
* @author Ken Krebs
* @author Juergen Hoeller
@ -68,7 +44,7 @@ public class JdbcVetRepositoryImpl implements VetRepository {
/**
* Refresh the cache of Vets that the ClinicService is holding.
* @see org.springframework.samples.petclinic.service.ClinicService#getVets()
* @see org.springframework.samples.petclinic.service.ClinicService#findVets()
*/
@ManagedOperation
@Transactional(readOnly = true)
@ -104,8 +80,7 @@ public class JdbcVetRepositoryImpl implements VetRepository {
}
}
@Transactional(readOnly = true)
public Collection<Vet> getVets() throws DataAccessException {
public Collection<Vet> findAll() throws DataAccessException {
synchronized (this.vets) {
if (this.vets.isEmpty()) {
refreshVetsCache();

View file

@ -6,34 +6,20 @@ import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.samples.petclinic.Pet;
import org.springframework.samples.petclinic.Visit;
import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* A simple JDBC-based implementation of the {@link ClinicService} interface.
*
* <p>This class uses Java 5 language features and the {@link SimpleJdbcTemplate}
* plus {@link SimpleJdbcInsert}. It also takes advantage of classes like
* {@link BeanPropertySqlParameterSource} and
* {@link ParameterizedBeanPropertyRowMapper} which provide automatic mapping
* between JavaBean properties and JDBC parameters or query results.
*
* <p>JdbcClinicImpl is a rewrite of the AbstractJdbcClinic which was the base
* class for JDBC implementations of the ClinicService interface for Spring 2.0.
*
* @author Ken Krebs
* @author Juergen Hoeller
@ -41,6 +27,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Sam Brannen
* @author Thomas Risberg
* @author Mark Fisher
* @author Michael Isvy
*/
@Service
public class JdbcVisitRepositoryImpl implements VisitRepository {
@ -59,8 +46,7 @@ public class JdbcVisitRepositoryImpl implements VisitRepository {
}
@Transactional
public void storeVisit(Visit visit) throws DataAccessException {
public void save(Visit visit) throws DataAccessException {
if (visit.isNew()) {
Number newKey = this.insertVisit.executeAndReturnKey(
createVisitParameterSource(visit));
@ -75,8 +61,6 @@ public class JdbcVisitRepositoryImpl implements VisitRepository {
this.jdbcTemplate.update("DELETE FROM pets WHERE id=?", id);
}
// END of ClinicService implementation section ************************************
/**
* Creates a {@link MapSqlParameterSource} based on data values from the
@ -108,24 +92,5 @@ public class JdbcVisitRepositoryImpl implements VisitRepository {
petId);
return visits;
}
/**
* {@link ParameterizedRowMapper} implementation mapping data from a
* {@link ResultSet} to the corresponding properties of the {@link JdbcPet} class.
*/
private class JdbcPetRowMapper implements ParameterizedRowMapper<JdbcPet> {
public JdbcPet mapRow(ResultSet rs, int rownum) throws SQLException {
JdbcPet pet = new JdbcPet();
pet.setId(rs.getInt("id"));
pet.setName(rs.getString("name"));
pet.setBirthDate(rs.getDate("birth_date"));
pet.setTypeId(rs.getInt("type_id"));
pet.setOwnerId(rs.getInt("owner_id"));
return pet;
}
}
}

View file

@ -1,86 +0,0 @@
package org.springframework.samples.petclinic.repository.jpa;
import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
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;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.dao.DataAccessException;
/**
* JPA implementation of the ClinicService interface using EntityManager.
*
* <p>The mappings are defined in "orm.xml" located in the META-INF directory.
*
* @author Mike Keith
* @author Rod Johnson
* @author Sam Brannen
* @author Michael Isvy
* @since 22.4.2006
*/
@Repository
@Transactional
public class JpaClinicImpl implements ClinicService {
@PersistenceContext
private EntityManager em;
@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
public Collection<Vet> getVets() {
return this.em.createQuery("SELECT vet FROM Vet vet ORDER BY vet.lastName, vet.firstName").getResultList();
}
@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
public Collection<PetType> getPetTypes() {
return this.em.createQuery("SELECT ptype FROM PetType ptype ORDER BY ptype.name").getResultList();
}
@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
public Collection<Owner> findOwners(String lastName) {
Query query = this.em.createQuery("SELECT owner FROM Owner owner WHERE owner.lastName LIKE :lastName");
query.setParameter("lastName", lastName + "%");
return query.getResultList();
}
@Transactional(readOnly = true)
public Owner findOwner(int id) {
return this.em.find(Owner.class, id);
}
@Transactional(readOnly = true)
public Pet findPet(int id) {
return this.em.find(Pet.class, id);
}
public void storeOwner(Owner owner) {
this.em.merge(owner);
}
public void storePet(Pet pet) {
this.em.merge(pet);
}
public void storeVisit(Visit visit) {
this.em.merge(visit);
}
public void deletePet(int id) throws DataAccessException {
Pet pet = findPet(id);
this.em.remove(pet);
}
}

View file

@ -6,12 +6,7 @@ import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.dao.DataAccessException;
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;
import org.springframework.samples.petclinic.repository.OwnerRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@ -35,7 +30,6 @@ public class JpaOwnerRepositoryImpl implements OwnerRepository {
private EntityManager em;
@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
public Collection<Owner> findByLastName(String lastName) {
Query query = this.em.createQuery("SELECT owner FROM Owner owner WHERE owner.lastName LIKE :lastName");
@ -43,7 +37,6 @@ public class JpaOwnerRepositoryImpl implements OwnerRepository {
return query.getResultList();
}
@Transactional(readOnly = true)
public Owner findById(int id) {
return this.em.find(Owner.class, id);
}

View file

@ -0,0 +1,43 @@
package org.springframework.samples.petclinic.repository.jpa;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.samples.petclinic.Pet;
import org.springframework.samples.petclinic.PetType;
import org.springframework.samples.petclinic.repository.PetRepository;
import org.springframework.stereotype.Repository;
/**
* JPA implementation of the ClinicService interface using EntityManager.
*
* <p>The mappings are defined in "orm.xml" located in the META-INF directory.
*
* @author Mike Keith
* @author Rod Johnson
* @author Sam Brannen
* @author Michael Isvy
* @since 22.4.2006
*/
@Repository
public class JpaPetRepositoryImpl implements PetRepository {
@PersistenceContext
private EntityManager em;
@SuppressWarnings("unchecked")
public List<PetType> findPetTypes() {
return this.em.createQuery("SELECT ptype FROM PetType ptype ORDER BY ptype.name").getResultList();
}
public Pet findById(int id) {
return this.em.find(Pet.class, id);
}
public void save(Pet pet) {
this.em.merge(pet);
}
}

View file

@ -0,0 +1,35 @@
package org.springframework.samples.petclinic.repository.jpa;
import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.samples.petclinic.Vet;
import org.springframework.samples.petclinic.repository.VetRepository;
import org.springframework.stereotype.Repository;
/**
* JPA implementation of the ClinicService interface using EntityManager.
*
* <p>The mappings are defined in "orm.xml" located in the META-INF directory.
*
* @author Mike Keith
* @author Rod Johnson
* @author Sam Brannen
* @author Michael Isvy
* @since 22.4.2006
*/
@Repository
public class JpaVetRepositoryImpl implements VetRepository {
@PersistenceContext
private EntityManager em;
@SuppressWarnings("unchecked")
public Collection<Vet> findAll() {
return this.em.createQuery("SELECT vet FROM Vet vet ORDER BY vet.lastName, vet.firstName").getResultList();
}
}

View file

@ -0,0 +1,46 @@
package org.springframework.samples.petclinic.repository.jpa;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.samples.petclinic.Visit;
import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.stereotype.Repository;
/**
* JPA implementation of the ClinicService interface using EntityManager.
*
* <p>The mappings are defined in "orm.xml" located in the META-INF directory.
*
* @author Mike Keith
* @author Rod Johnson
* @author Sam Brannen
* @author Michael Isvy
* @since 22.4.2006
*/
@Repository
public class JpaVisitRepositoryImpl implements VisitRepository {
@PersistenceContext
private EntityManager em;
public void save(Visit visit) {
this.em.merge(visit);
}
@Override
@SuppressWarnings("unchecked")
public List<Visit> findByPetId(Integer petId) {
Query query = this.em.createQuery("SELECT visit FROM Visit v where v.pets.id= :id");
query.setParameter("id", petId);
return query.getResultList();
}
}

View file

@ -1,45 +0,0 @@
package org.springframework.samples.petclinic.repository.jpa;
import java.util.Collection;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
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;
import org.springframework.samples.petclinic.service.ClinicService;
/**
*
* @author Michael Isvy
* @since 15.1.2013
*/
public interface SpringDataClinic extends ClinicService, 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

@ -1,4 +1,4 @@
package org.springframework.samples.petclinic.repository.jpa;
package org.springframework.samples.petclinic.repository.springdatajpa;
import org.springframework.data.repository.Repository;
import org.springframework.samples.petclinic.Owner;

View file

@ -0,0 +1,21 @@
package org.springframework.samples.petclinic.repository.springdatajpa;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.samples.petclinic.Pet;
import org.springframework.samples.petclinic.PetType;
import org.springframework.samples.petclinic.repository.PetRepository;
/**
*
* @author Michael Isvy
* @since 15.1.2013
*/
public interface SpringDataPetRepository extends PetRepository, Repository<Pet, Integer> {
@Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name")
List<PetType> findPetTypes() throws DataAccessException;
}

View file

@ -0,0 +1,13 @@
package org.springframework.samples.petclinic.repository.springdatajpa;
import org.springframework.data.repository.Repository;
import org.springframework.samples.petclinic.Vet;
import org.springframework.samples.petclinic.repository.VetRepository;
/**
*
* @author Michael Isvy
* @since 15.1.2013
*/
public interface SpringDataVetRepository extends VetRepository, Repository<Vet, Integer> {
}

View file

@ -0,0 +1,13 @@
package org.springframework.samples.petclinic.repository.springdatajpa;
import org.springframework.data.repository.Repository;
import org.springframework.samples.petclinic.Visit;
import org.springframework.samples.petclinic.repository.VisitRepository;
/**
*
* @author Michael Isvy
* @since 15.1.2013
*/
public interface SpringDataVisitRepository extends VisitRepository, Repository<Visit, Integer> {
}

View file

@ -22,18 +22,16 @@ import org.springframework.samples.petclinic.Visit;
*/
public interface ClinicService {
public Collection<PetType> getPetTypes() throws DataAccessException;
public Collection<PetType> findPetTypes() throws DataAccessException;
public Owner findOwnerById(int id) throws DataAccessException;
public Pet findPetById(int id) throws DataAccessException;
public void storePet(Pet pet) throws DataAccessException;
public void savePet(Pet pet) throws DataAccessException;
public void deletePet(int id) throws DataAccessException;
public void saveVisit(Visit visit) throws DataAccessException;
public void storeVisit(Visit visit) throws DataAccessException;
public Collection<Vet> getVets() throws DataAccessException;
public Collection<Vet> findVets() throws DataAccessException;
}

View file

@ -14,6 +14,7 @@ import org.springframework.samples.petclinic.repository.PetRepository;
import org.springframework.samples.petclinic.repository.VetRepository;
import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class ClinicServiceImpl implements ClinicService {
@ -30,32 +31,34 @@ public class ClinicServiceImpl implements ClinicService {
@Autowired
private VisitRepository visitRepository;
public Collection<PetType> getPetTypes() throws DataAccessException {
return petRepository.getPetTypes();
@Transactional(readOnly=true)
public Collection<PetType> findPetTypes() throws DataAccessException {
return petRepository.findPetTypes();
}
@Transactional(readOnly=true)
public Owner findOwnerById(int id) throws DataAccessException {
return ownerRepository.findById(id);
}
public void storeVisit(Visit visit) throws DataAccessException {
visitRepository.storeVisit(visit);
@Transactional
public void saveVisit(Visit visit) throws DataAccessException {
visitRepository.save(visit);
}
@Transactional(readOnly=true)
public Pet findPetById(int id) throws DataAccessException {
return petRepository.findById(id);
}
public void storePet(Pet pet) throws DataAccessException {
petRepository.storePet(pet);
@Transactional
public void savePet(Pet pet) throws DataAccessException {
petRepository.save(pet);
}
public void deletePet(int id) throws DataAccessException {
petRepository.deletePet(id);
}
public Collection<Vet> getVets() throws DataAccessException {
return vetRepository.getVets();
@Transactional(readOnly=true)
public Collection<Vet> findVets() throws DataAccessException {
return vetRepository.findAll();
}

View file

@ -43,7 +43,7 @@ public class PetController {
@ModelAttribute("types")
public Collection<PetType> populatePetTypes() {
return this.clinicService.getPetTypes();
return this.clinicService.findPetTypes();
}
@InitBinder
@ -67,7 +67,7 @@ public class PetController {
return "pets/createOrUpdatePetForm";
}
else {
this.clinicService.storePet(pet);
this.clinicService.savePet(pet);
status.setComplete();
return "redirect:/owners/" + pet.getOwner().getId();
}
@ -88,17 +88,10 @@ public class PetController {
return "pets/createOrUpdatePetForm";
}
else {
this.clinicService.storePet(pet);
this.clinicService.savePet(pet);
status.setComplete();
return "redirect:/owners/" + pet.getOwner().getId();
}
}
@RequestMapping(value="/owners/*/pets/{petId}/edit", method = RequestMethod.DELETE)
public String deletePet(@PathVariable("petId") int petId) {
Pet pet = this.clinicService.findPetById(petId);
this.clinicService.deletePet(petId);
return "redirect:/owners/" + pet.getOwner().getId();
}
}

View file

@ -20,7 +20,7 @@ public class PetTypeEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
for (PetType type : this.clinicService.getPetTypes()) {
for (PetType type : this.clinicService.findPetTypes()) {
if (type.getName().equals(text)) {
setValue(type);
}

View file

@ -42,7 +42,7 @@ public class VetController {
@RequestMapping("/vets")
public String showVetList(Model model) {
Vets vets = new Vets();
vets.getVetList().addAll(this.clinicService.getVets());
vets.getVetList().addAll(this.clinicService.findVets());
model.addAttribute("vets", vets);
return "vetsList";
}

View file

@ -59,7 +59,7 @@ public class VisitController {
return "pets/createOrUpdateVisitForm";
}
else {
this.clinicService.storeVisit(visit);
this.clinicService.saveVisit(visit);
status.setComplete();
return "redirect:/owners/" + visit.getPet().getOwner().getId();
}

View file

@ -20,16 +20,17 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.samples.petclinic.Visit;
import org.springframework.web.servlet.view.feed.AbstractAtomFeedView;
import com.sun.syndication.feed.atom.Content;
import com.sun.syndication.feed.atom.Entry;
import com.sun.syndication.feed.atom.Feed;
import org.springframework.samples.petclinic.Visit;
import org.springframework.web.servlet.view.feed.AbstractAtomFeedView;
/**
* A view creating a Atom representation from a list of Visit objects.
*

View file

@ -11,10 +11,6 @@
</layout>
</appender>
<logger name="org.springframework.samples.petclinic.aspects">
<level value="info" />
</logger>
<logger name="org.springframework.web">
<level value="info" />
</logger>
@ -23,8 +19,6 @@
<level value="info" />
</logger>
<!-- Root Logger -->
<root>
<priority value="info" /><!--

View file

@ -120,7 +120,7 @@
</beans>
<beans profile="spring-data-jpa">
<jpa:repositories base-package="org.springframework.samples.petclinic.repository.jpa"/>
<jpa:repositories base-package="org.springframework.samples.petclinic.repository.springdatajpa"/>
</beans>
</beans>

View file

@ -55,9 +55,6 @@
</fieldset>
</form:form>
<c:if test="${!pet['new']}">
<form:form method="delete">
<p class="submit"><input type="submit" value="Delete Pet"/></p>
</form:form>
</c:if>
<jsp:include page="../footer.jsp"/>
</div>

View file

@ -1,15 +1,13 @@
package org.springframework.samples.petclinic;
import java.util.Collection;
import java.util.Date;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import java.util.Collection;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.repository.OwnerRepository;
import org.springframework.samples.petclinic.util.EntityUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.transaction.annotation.Transactional;
@ -117,7 +115,7 @@ public abstract class AbstractOwnerRepositoryTests {
o1.getPets();
}
@Test
@Test @Transactional
public void insertOwner() {
Collection<Owner> owners = this.ownerRepository.findByLastName("Schultz");
int found = owners.size();
@ -132,7 +130,7 @@ public abstract class AbstractOwnerRepositoryTests {
assertEquals("Verifying number of owners after inserting a new one.", found + 1, owners.size());
}
@Test
@Test @Transactional
public void updateOwner() throws Exception {
Owner o1 = this.ownerRepository.findById(1);
String old = o1.getLastName();

View file

@ -1,12 +1,12 @@
package org.springframework.samples.petclinic;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Collection;
import java.util.Date;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.repository.OwnerRepository;
import org.springframework.samples.petclinic.repository.PetRepository;
@ -95,7 +95,7 @@ public abstract class AbstractPetRepositoryTests {
@Test
public void getPetTypes() {
Collection<PetType> petTypes = this.petRepository.getPetTypes();
Collection<PetType> petTypes = this.petRepository.findPetTypes();
PetType t1 = EntityUtils.getById(petTypes, PetType.class, 1);
assertEquals("cat", t1.getName());
@ -105,7 +105,7 @@ public abstract class AbstractPetRepositoryTests {
@Test
public void findPet() {
Collection<PetType> types = this.petRepository.getPetTypes();
Collection<PetType> types = this.petRepository.findPetTypes();
Pet p7 = this.petRepository.findById(7);
assertTrue(p7.getName().startsWith("Samantha"));
assertEquals(EntityUtils.getById(types, PetType.class, 1).getId(), p7.getType().getId());
@ -122,24 +122,24 @@ public abstract class AbstractPetRepositoryTests {
int found = o6.getPets().size();
Pet pet = new Pet();
pet.setName("bowser");
Collection<PetType> types = this.petRepository.getPetTypes();
Collection<PetType> types = this.petRepository.findPetTypes();
pet.setType(EntityUtils.getById(types, PetType.class, 2));
pet.setBirthDate(new Date());
o6.addPet(pet);
assertEquals(found + 1, o6.getPets().size());
// both storePet and storeOwner are necessary to cover all ORM tools
this.petRepository.storePet(pet);
this.petRepository.save(pet);
this.ownerRepository.save(o6);
o6 = this.ownerRepository.findById(6);
assertEquals(found + 1, o6.getPets().size());
}
@Test
@Test @Transactional
public void updatePet() throws Exception {
Pet p7 = this.petRepository.findById(7);
String old = p7.getName();
p7.setName(old + "X");
this.petRepository.storePet(p7);
this.petRepository.save(p7);
p7 = this.petRepository.findById(7);
assertEquals(old + "X", p7.getName());
}

View file

@ -1,12 +1,10 @@
package org.springframework.samples.petclinic;
import java.util.Collection;
import java.util.Date;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import java.util.Collection;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.repository.VetRepository;
import org.springframework.samples.petclinic.service.ClinicService;
@ -90,8 +88,8 @@ public abstract class AbstractVetRepositoryTests {
@Test @Transactional
public void getVets() {
Collection<Vet> vets = this.vetRepository.getVets();
public void findVets() {
Collection<Vet> vets = this.vetRepository.findAll();
Vet v1 = EntityUtils.getById(vets, Vet.class, 2);
assertEquals("Leary", v1.getLastName());

View file

@ -1,17 +1,12 @@
package org.springframework.samples.petclinic;
import java.util.Collection;
import java.util.Date;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.repository.PetRepository;
import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.samples.petclinic.util.EntityUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.transaction.annotation.Transactional;
@ -101,8 +96,8 @@ public abstract class AbstractVisitRepositoryTests {
p7.addVisit(visit);
visit.setDescription("test");
// both storeVisit and storePet are necessary to cover all ORM tools
this.visitRepository.storeVisit(visit);
this.petRepository.storePet(p7);
this.visitRepository.save(visit);
this.petRepository.save(p7);
// assertTrue(!visit.isNew()); -- NOT TRUE FOR TOPLINK (before commit)
p7 = this.petRepository.findById(7);
assertEquals(found + 1, p7.getVisits().size());

View file

@ -2,7 +2,6 @@ package org.springframework.samples.petclinic.jdbc;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractOwnerRepositoryTests;
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

View file

@ -2,7 +2,6 @@ package org.springframework.samples.petclinic.jdbc;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractPetRepositoryTests;
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

View file

@ -2,7 +2,6 @@ package org.springframework.samples.petclinic.jdbc;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractVetRepositoryTests;
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

View file

@ -2,7 +2,6 @@ package org.springframework.samples.petclinic.jdbc;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractVisitRepositoryTests;
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

View file

@ -2,7 +2,6 @@ package org.springframework.samples.petclinic.jpa;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractPetRepositoryTests;
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

View file

@ -2,7 +2,6 @@ package org.springframework.samples.petclinic.jpa;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractVetRepositoryTests;
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

View file

@ -2,7 +2,6 @@ package org.springframework.samples.petclinic.jpa;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractVisitRepositoryTests;
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

View file

@ -0,0 +1,34 @@
package org.springframework.samples.petclinic.springdatajpa;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractOwnerRepositoryTests;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* <p>
* Provides the following services:
* <ul>
* <li>Injects test dependencies, meaning that we don't need to perform
* application context lookups. See the setClinic() method. Injection uses
* autowiring by type.</li>
* <li>Executes each test method in its own transaction, which is automatically
* rolled back by default. This means that even if tests insert or otherwise
* change database state, there is no need for a teardown or cleanup script.</li>
* </ul>
* <p>
* </p>
*
* @author Rod Johnson
* @author Sam Brannen
* @author Michael Isvy
*/
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles({"jpa","spring-data-jpa"})
public class JpaOwnerRepositoryImplTests extends AbstractOwnerRepositoryTests {
}

View file

@ -0,0 +1,28 @@
package org.springframework.samples.petclinic.springdatajpa;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractPetRepositoryTests;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* <p>
* Integration tests for the {@link JdbcClinicImpl} implementation.
* </p>
* <p>
* </p>
*
* @author Thomas Risberg
* @author Michael Isvy
*/
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@ActiveProfiles({"jpa","spring-data-jpa"})
public class JpaPetRepositoryImplTests extends AbstractPetRepositoryTests {
}

View file

@ -0,0 +1,28 @@
package org.springframework.samples.petclinic.springdatajpa;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractVetRepositoryTests;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* <p>
* Integration tests for the {@link JdbcClinicImpl} implementation.
* </p>
* <p>
* </p>
*
* @author Thomas Risberg
* @author Michael Isvy
*/
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@ActiveProfiles({"jpa","spring-data-jpa"})
public class JpaVetRepositoryImplTests extends AbstractVetRepositoryTests {
}

View file

@ -0,0 +1,28 @@
package org.springframework.samples.petclinic.springdatajpa;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractVisitRepositoryTests;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* <p>
* Integration tests for the {@link JdbcClinicImpl} implementation.
* </p>
* <p>
* </p>
*
* @author Thomas Risberg
* @author Michael Isvy
*/
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@ActiveProfiles({"jpa","spring-data-jpa"})
public class JpaVisitRepositoryImplTests extends AbstractVisitRepositoryTests {
}

View file

@ -1,5 +1,5 @@
package org.springframework.samples.petclinic.jpa;
package org.springframework.samples.petclinic.springdatajpa;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractOwnerRepositoryTests;

View file

@ -16,23 +16,24 @@
package org.springframework.samples.petclinic.web;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.sun.syndication.feed.atom.Entry;
import com.sun.syndication.feed.atom.Feed;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Before;
import org.junit.Test;
import org.springframework.samples.petclinic.Pet;
import org.springframework.samples.petclinic.PetType;
import org.springframework.samples.petclinic.Visit;
import com.sun.syndication.feed.atom.Entry;
import com.sun.syndication.feed.atom.Feed;
/**
* @author Arjen Poutsma
*/