diff --git a/src/main/java/org/springframework/samples/petclinic/model/Pet.java b/src/main/java/org/springframework/samples/petclinic/model/Pet.java
index 4bc2b92f7..ebd231c88 100644
--- a/src/main/java/org/springframework/samples/petclinic/model/Pet.java
+++ b/src/main/java/org/springframework/samples/petclinic/model/Pet.java
@@ -80,7 +80,7 @@ public class Pet extends NamedEntity {
return this.type;
}
- protected void setOwner(Owner owner) {
+ public void setOwner(Owner owner) {
this.owner = owner;
}
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 693b2e5e9..b74a9597c 100644
--- a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java
+++ b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java
@@ -15,6 +15,7 @@
*/
package org.springframework.samples.petclinic.repository;
+import java.util.Collection;
import java.util.List;
import org.springframework.dao.DataAccessException;
@@ -50,6 +51,16 @@ public interface PetRepository {
*/
Pet findById(int id) throws DataAccessException;
+ /**
+ * Retrieve a list ofPet
from the data store by name.
+ *
+ * @param name the name to search for
+ * @return the Pet
if found
+ * @throws org.springframework.dao.DataRetrievalFailureException
+ * if not found
+ */
+ Collection findByName(String name) throws DataAccessException;
+
/**
* Save a Pet
to the data store, either inserting or updating it.
*
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 c594ead1b..5bfc7999f 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
@@ -15,6 +15,7 @@
*/
package org.springframework.samples.petclinic.repository.jdbc;
+import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -129,5 +130,24 @@ public class JdbcPetRepositoryImpl implements PetRepository {
.addValue("type_id", pet.getType().getId())
.addValue("owner_id", pet.getOwner().getId());
}
+
+ @Override
+ public Collection findByName(String name) throws DataAccessException {
+
+ Map params = new HashMap();
+ params.put("name", "%" + name + "%");
+ List pets = this.namedParameterJdbcTemplate.query(
+ "SELECT id, birth_date, type_id, owner_id FROM pets WHERE name like :name",
+ params,
+ ParameterizedBeanPropertyRowMapper.newInstance(Pet.class)
+ );
+ for (Pet pet : pets) {
+ if(pet.getName().equals(null)){
+ new Exception();
+ }
+ }
+ return pets;
+
+ }
}
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
index 84d564da4..56c1aede7 100644
--- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryImpl.java
+++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryImpl.java
@@ -15,11 +15,16 @@
*/
package org.springframework.samples.petclinic.repository.jpa;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
+import javax.persistence.Query;
+import org.joda.time.DateTime;
+import org.springframework.samples.petclinic.model.Owner;
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.repository.PetRepository;
@@ -60,5 +65,25 @@ public class JpaPetRepositoryImpl implements PetRepository {
this.em.merge(pet);
}
}
+
+ @Override
+ public Collection findByName(String name) {
+ Query query = this.em.createQuery("SELECT pet.id, pet.name,pet.birthDate,pet.type,pet.owner FROM Pet pet inner join pet.owner as owner WHERE pet.name like :name");
+ query.setParameter("name", "%"+ name + "%");
+ Collection pets = new ArrayList();
+ @SuppressWarnings("unchecked")
+ List