diff --git a/src/main/java/org/springframework/samples/petclinic/Person.java b/src/main/java/org/springframework/samples/petclinic/Person.java index 06b03840f..d42bf04a0 100644 --- a/src/main/java/org/springframework/samples/petclinic/Person.java +++ b/src/main/java/org/springframework/samples/petclinic/Person.java @@ -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; diff --git a/src/main/java/org/springframework/samples/petclinic/Vets.java b/src/main/java/org/springframework/samples/petclinic/Vets.java index 2e3b25e27..cae4b7757 100644 --- a/src/main/java/org/springframework/samples/petclinic/Vets.java +++ b/src/main/java/org/springframework/samples/petclinic/Vets.java @@ -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; diff --git a/src/main/java/org/springframework/samples/petclinic/aspects/AbstractTraceAspect.java b/src/main/java/org/springframework/samples/petclinic/aspects/AbstractTraceAspect.java deleted file mode 100644 index 977bb684f..000000000 --- a/src/main/java/org/springframework/samples/petclinic/aspects/AbstractTraceAspect.java +++ /dev/null @@ -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()); - } - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/aspects/CallMonitoringAspect.java b/src/main/java/org/springframework/samples/petclinic/aspects/CallMonitoringAspect.java index 2de4cb41d..c6d88878c 100644 --- a/src/main/java/org/springframework/samples/petclinic/aspects/CallMonitoringAspect.java +++ b/src/main/java/org/springframework/samples/petclinic/aspects/CallMonitoringAspect.java @@ -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; diff --git a/src/main/java/org/springframework/samples/petclinic/aspects/UsageLogAspect.java b/src/main/java/org/springframework/samples/petclinic/aspects/UsageLogAspect.java index b7fde3779..a8a4d51e1 100644 --- a/src/main/java/org/springframework/samples/petclinic/aspects/UsageLogAspect.java +++ b/src/main/java/org/springframework/samples/petclinic/aspects/UsageLogAspect.java @@ -30,7 +30,7 @@ public class UsageLogAspect { this.namesRequested = new ArrayList(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 diff --git a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java index 119e0e31b..68656ba6c 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java @@ -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 PetTypes from the data store. * @return a Collection of PetTypes */ - Collection getPetTypes() throws DataAccessException; + List findPetTypes() throws DataAccessException; /** * Retrieve a Pet from the data store by id. @@ -41,11 +38,6 @@ public interface PetRepository { * @param pet the Pet to save * @see BaseEntity#isNew */ - void storePet(Pet pet) throws DataAccessException; - - /** - * Deletes a Pet from the data store. - */ - void deletePet(int id) throws DataAccessException; + void save(Pet pet) throws DataAccessException; } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java index a3068601e..3fe4ff115 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java @@ -21,7 +21,7 @@ public interface VetRepository { * Retrieve all Vets from the data store. * @return a Collection of Vets */ - Collection getVets() throws DataAccessException; + Collection findAll() throws DataAccessException; } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java index 2af5ac743..d3e7f8d56 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java @@ -23,7 +23,7 @@ public interface VisitRepository { * @param visit the Visit to save * @see BaseEntity#isNew */ - void storeVisit(Visit visit) throws DataAccessException; + void save(Visit visit) throws DataAccessException; List findByPetId(Integer petId); diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcClinicImplMBean.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcClinicImplMBean.java deleted file mode 100644 index b3240360a..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcClinicImplMBean.java +++ /dev/null @@ -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(); - -} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java index eef9dfdef..7c283140e 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java @@ -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. * - *

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

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 diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java index 6efff5f5b..34b85217d 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java @@ -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. - * - *

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

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 vets = new ArrayList(); - - @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 getPetTypes() throws DataAccessException { + public List 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 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}. - */ - - - - } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java index 88b8c0301..374595a48 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java @@ -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. - * - *

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

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 getVets() throws DataAccessException { + public Collection findAll() throws DataAccessException { synchronized (this.vets) { if (this.vets.isEmpty()) { refreshVetsCache(); diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java index f1cc60bcc..ffa6ce12e 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java @@ -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. * - *

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

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 { - - 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; - } - } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaClinicImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaClinicImpl.java deleted file mode 100644 index 361fd4b36..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaClinicImpl.java +++ /dev/null @@ -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. - * - *

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 getVets() { - return this.em.createQuery("SELECT vet FROM Vet vet ORDER BY vet.lastName, vet.firstName").getResultList(); - } - - @Transactional(readOnly = true) - @SuppressWarnings("unchecked") - public Collection getPetTypes() { - return this.em.createQuery("SELECT ptype FROM PetType ptype ORDER BY ptype.name").getResultList(); - } - - @Transactional(readOnly = true) - @SuppressWarnings("unchecked") - public Collection 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); - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaOwnerRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaOwnerRepositoryImpl.java index 73ee82ac1..b7a0b7190 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaOwnerRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaOwnerRepositoryImpl.java @@ -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 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); } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryImpl.java new file mode 100644 index 000000000..9a131f072 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryImpl.java @@ -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. + * + *

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 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); + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVetRepositoryImpl.java new file mode 100644 index 000000000..387a29e65 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVetRepositoryImpl.java @@ -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. + * + *

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 findAll() { + return this.em.createQuery("SELECT vet FROM Vet vet ORDER BY vet.lastName, vet.firstName").getResultList(); + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryImpl.java new file mode 100644 index 000000000..ddca8208f --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryImpl.java @@ -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. + * + *

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 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(); + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/SpringDataClinic.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/SpringDataClinic.java deleted file mode 100644 index cf1af4b53..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/SpringDataClinic.java +++ /dev/null @@ -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 getVets(); - - @Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name") - public Collection getPetTypes(); - - @Query("SELECT owner FROM Owner owner WHERE owner.lastName LIKE :lastName") - public Collection 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); - -} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/SpringDataOwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataOwnerRepository.java similarity index 82% rename from src/main/java/org/springframework/samples/petclinic/repository/jpa/SpringDataOwnerRepository.java rename to src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataOwnerRepository.java index f32b34ced..ec1e91f1b 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/SpringDataOwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataOwnerRepository.java @@ -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; diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetRepository.java new file mode 100644 index 000000000..dd735447f --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetRepository.java @@ -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 { + + @Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name") + List findPetTypes() throws DataAccessException; +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVetRepository.java new file mode 100644 index 000000000..5a9269040 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVetRepository.java @@ -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 { +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVisitRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVisitRepository.java new file mode 100644 index 000000000..77f072b14 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVisitRepository.java @@ -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 { +} diff --git a/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java b/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java index 24a932946..c85f1bf83 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java @@ -22,18 +22,16 @@ import org.springframework.samples.petclinic.Visit; */ public interface ClinicService { - public Collection getPetTypes() throws DataAccessException; + public Collection 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 getVets() throws DataAccessException; + public Collection findVets() throws DataAccessException; } diff --git a/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java b/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java index b4e51e771..cf0f0a980 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java @@ -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 getPetTypes() throws DataAccessException { - return petRepository.getPetTypes(); + @Transactional(readOnly=true) + public Collection 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 getVets() throws DataAccessException { - return vetRepository.getVets(); + @Transactional(readOnly=true) + public Collection findVets() throws DataAccessException { + return vetRepository.findAll(); } diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetController.java b/src/main/java/org/springframework/samples/petclinic/web/PetController.java index b440a79bf..ad94356ac 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/PetController.java @@ -43,7 +43,7 @@ public class PetController { @ModelAttribute("types") public Collection 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(); - } - } diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetTypeEditor.java b/src/main/java/org/springframework/samples/petclinic/web/PetTypeEditor.java index 1be37c98c..801442576 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/PetTypeEditor.java +++ b/src/main/java/org/springframework/samples/petclinic/web/PetTypeEditor.java @@ -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); } diff --git a/src/main/java/org/springframework/samples/petclinic/web/VetController.java b/src/main/java/org/springframework/samples/petclinic/web/VetController.java index 5bcdfa299..996e587f8 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/VetController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/VetController.java @@ -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"; } diff --git a/src/main/java/org/springframework/samples/petclinic/web/VisitController.java b/src/main/java/org/springframework/samples/petclinic/web/VisitController.java index f093c04d6..d2f1e6026 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/VisitController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/VisitController.java @@ -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(); } diff --git a/src/main/java/org/springframework/samples/petclinic/web/VisitsAtomView.java b/src/main/java/org/springframework/samples/petclinic/web/VisitsAtomView.java index f5f69e0fd..4fbfe0bbc 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/VisitsAtomView.java +++ b/src/main/java/org/springframework/samples/petclinic/web/VisitsAtomView.java @@ -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. * diff --git a/src/main/resources/log4j.xml b/src/main/resources/log4j.xml index b7d82f5d9..3f0299af0 100755 --- a/src/main/resources/log4j.xml +++ b/src/main/resources/log4j.xml @@ -11,10 +11,6 @@ - - - - @@ -23,8 +19,6 @@ - -