mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-05-29 14:49:38 +00:00
Improvements in VisitRepository.findByPetId implementation.
- In the Jdbc implementation: pets belonging to a visit were not added. - In the Jpa implementation: query variable was wrong. - Test case: AbstractClinicServiceTests.shouldFindVisitsByPetId()
This commit is contained in:
parent
817fabd9ea
commit
ca755be44a
5 changed files with 40 additions and 6 deletions
|
@ -19,13 +19,17 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.dao.DataAccessException;
|
import org.springframework.dao.DataAccessException;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
||||||
|
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||||
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
|
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
|
||||||
import org.springframework.samples.petclinic.model.Visit;
|
import org.springframework.samples.petclinic.model.Visit;
|
||||||
import org.springframework.samples.petclinic.repository.VisitRepository;
|
import org.springframework.samples.petclinic.repository.VisitRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple JDBC-based implementation of the {@link VisitRepository} interface.
|
* A simple JDBC-based implementation of the {@link VisitRepository} interface.
|
||||||
|
@ -41,13 +45,13 @@ import java.util.List;
|
||||||
@Repository
|
@Repository
|
||||||
public class JdbcVisitRepositoryImpl implements VisitRepository {
|
public class JdbcVisitRepositoryImpl implements VisitRepository {
|
||||||
|
|
||||||
private JdbcTemplate jdbcTemplate;
|
private NamedParameterJdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
private SimpleJdbcInsert insertVisit;
|
private SimpleJdbcInsert insertVisit;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public JdbcVisitRepositoryImpl(DataSource dataSource) {
|
public JdbcVisitRepositoryImpl(DataSource dataSource) {
|
||||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
||||||
|
|
||||||
this.insertVisit = new SimpleJdbcInsert(dataSource)
|
this.insertVisit = new SimpleJdbcInsert(dataSource)
|
||||||
.withTableName("visits")
|
.withTableName("visits")
|
||||||
|
@ -80,9 +84,22 @@ public class JdbcVisitRepositoryImpl implements VisitRepository {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Visit> findByPetId(Integer petId) {
|
public List<Visit> findByPetId(Integer petId) {
|
||||||
return this.jdbcTemplate.query(
|
Map<String, Object> params = new HashMap<>();
|
||||||
"SELECT id as visit_id, visit_date, description FROM visits WHERE pet_id=?",
|
params.put("id", petId);
|
||||||
new JdbcVisitRowMapper(), petId);
|
JdbcPet pet = this.jdbcTemplate.queryForObject(
|
||||||
|
"SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id",
|
||||||
|
params,
|
||||||
|
new JdbcPetRowMapper());
|
||||||
|
|
||||||
|
List<Visit> visits = this.jdbcTemplate.query(
|
||||||
|
"SELECT id as visit_id, visit_date, description FROM visits WHERE pet_id=:id",
|
||||||
|
params, new JdbcVisitRowMapper());
|
||||||
|
|
||||||
|
for (Visit visit: visits) {
|
||||||
|
visit.setPet(pet);
|
||||||
|
}
|
||||||
|
|
||||||
|
return visits;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,7 +56,7 @@ public class JpaVisitRepositoryImpl implements VisitRepository {
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public List<Visit> findByPetId(Integer petId) {
|
public List<Visit> findByPetId(Integer petId) {
|
||||||
Query query = this.em.createQuery("SELECT visit FROM Visit v where v.pets.id= :id");
|
Query query = this.em.createQuery("SELECT v FROM Visit v where v.pet.id= :id");
|
||||||
query.setParameter("id", petId);
|
query.setParameter("id", petId);
|
||||||
return query.getResultList();
|
return query.getResultList();
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,4 +48,6 @@ public interface ClinicService {
|
||||||
|
|
||||||
Collection<Owner> findOwnerByLastName(String lastName) throws DataAccessException;
|
Collection<Owner> findOwnerByLastName(String lastName) throws DataAccessException;
|
||||||
|
|
||||||
|
Collection<Visit> findVisitsByPetId(int petId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,5 +105,10 @@ public class ClinicServiceImpl implements ClinicService {
|
||||||
return vetRepository.findAll();
|
return vetRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<Visit> findVisitsByPetId(int petId) {
|
||||||
|
return visitRepository.findByPetId(petId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -190,5 +190,15 @@ public abstract class AbstractClinicServiceTests {
|
||||||
assertThat(visit.getId()).isNotNull();
|
assertThat(visit.getId()).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldFindVisitsByPetId() throws Exception {
|
||||||
|
Collection<Visit> visits = this.clinicService.findVisitsByPetId(7);
|
||||||
|
assertThat(visits.size()).isEqualTo(2);
|
||||||
|
Visit[] visitArr = visits.toArray(new Visit[visits.size()]);
|
||||||
|
assertThat(visitArr[0].getPet()).isNotNull();
|
||||||
|
assertThat(visitArr[0].getDate()).isNotNull();
|
||||||
|
assertThat(visitArr[0].getPet().getId()).isEqualTo(7);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue