diff --git a/pom.xml b/pom.xml
index b929a6447..fd3b8896a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -18,6 +18,7 @@
4.1.6.RELEASE
1.8.0.RELEASE
+ 1.1.0.RELEASE
@@ -127,6 +128,19 @@
+
+ org.springframework.data
+ spring-data-jdbc-core
+ ${spring-data-jdbc.version}
+
+
+ org.springframework
+ *
+
+
+
+
+
org.springframework
spring-jdbc
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 579de5284..6759c5836 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
@@ -116,18 +116,12 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
Map params = new HashMap();
params.put("id", owner.getId().intValue());
final List pets = this.namedParameterJdbcTemplate.query(
- "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE owner_id=:id",
+ "SELECT pets.id, name, birth_date, type_id, owner_id, visits.id, visit_date, description, pet_id FROM pets LEFT OUTER JOIN visits ON pets.id = pet_id WHERE owner_id=:id",
params,
- new JdbcPetRowMapper()
+ new JdbcPetVisitExtractor()
);
for (JdbcPet pet : pets) {
owner.addPet(pet);
- // Pet types have not been loaded at this stage. They are loaded separately
- pet.setType(EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId()));
- List visits = this.visitRepository.findByPetId(pet.getId());
- for (Visit visit : visits) {
- pet.addVisit(visit);
- }
}
}
diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRowMapper.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRowMapper.java
index 4164f746f..28151a723 100644
--- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRowMapper.java
+++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRowMapper.java
@@ -31,7 +31,7 @@ class JdbcPetRowMapper extends BeanPropertyRowMapper {
@Override
public JdbcPet mapRow(ResultSet rs, int rownum) throws SQLException {
JdbcPet pet = new JdbcPet();
- pet.setId(rs.getInt("id"));
+ pet.setId(rs.getInt("pets.id"));
pet.setName(rs.getString("name"));
Date birthDate = rs.getDate("birth_date");
pet.setBirthDate(new DateTime(birthDate));
diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetVisitExtractor.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetVisitExtractor.java
new file mode 100644
index 000000000..922d608fe
--- /dev/null
+++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetVisitExtractor.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2002-2015 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.samples.petclinic.repository.jdbc;
+
+import org.springframework.data.jdbc.core.OneToManyResultSetExtractor;
+import org.springframework.samples.petclinic.model.Visit;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+public class JdbcPetVisitExtractor extends
+ OneToManyResultSetExtractor {
+
+ public JdbcPetVisitExtractor() {
+ super(new JdbcPetRowMapper(), new JdbcVisitRowMapper());
+ }
+
+ @Override
+ protected Integer mapPrimaryKey(ResultSet rs) throws SQLException {
+ return rs.getInt("pets.id");
+ }
+
+ @Override
+ protected Integer mapForeignKey(ResultSet rs) throws SQLException {
+ if (rs.getObject("visits.pet_id") == null) {
+ return null;
+ }
+ else {
+ return rs.getInt("visits.pet_id");
+ }
+ }
+
+ @Override
+ protected void addChild(JdbcPet root, Visit child) {
+ root.addVisit(child);
+ }
+}
\ No newline at end of file
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 b6a004561..19210574f 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
@@ -15,17 +15,8 @@
*/
package org.springframework.samples.petclinic.repository.jdbc;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.Date;
-import java.util.List;
-
-import javax.sql.DataSource;
-
-import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
-import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
@@ -33,6 +24,9 @@ import org.springframework.samples.petclinic.model.Visit;
import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.stereotype.Repository;
+import javax.sql.DataSource;
+import java.util.List;
+
/**
* A simple JDBC-based implementation of the {@link VisitRepository} interface.
*
@@ -90,21 +84,9 @@ public class JdbcVisitRepositoryImpl implements VisitRepository {
@Override
public List findByPetId(Integer petId) {
- final List visits = this.jdbcTemplate.query(
- "SELECT id, visit_date, description FROM visits WHERE pet_id=?",
- new BeanPropertyRowMapper() {
- @Override
- public Visit mapRow(ResultSet rs, int row) throws SQLException {
- Visit visit = new Visit();
- visit.setId(rs.getInt("id"));
- Date visitDate = rs.getDate("visit_date");
- visit.setDate(new DateTime(visitDate));
- visit.setDescription(rs.getString("description"));
- return visit;
- }
- },
- petId);
- return visits;
+ return this.jdbcTemplate.query(
+ "SELECT id as visit_id, visit_date, description FROM visits WHERE pet_id=?",
+ new JdbcVisitRowMapper(), petId);
}
}
diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRowMapper.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRowMapper.java
new file mode 100644
index 000000000..241a7902b
--- /dev/null
+++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRowMapper.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2002-2015 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.samples.petclinic.repository.jdbc;
+
+
+import org.joda.time.DateTime;
+import org.springframework.jdbc.core.RowMapper;
+import org.springframework.samples.petclinic.model.Visit;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Date;
+
+class JdbcVisitRowMapper implements RowMapper {
+
+ @Override
+ public Visit mapRow(ResultSet rs, int row) throws SQLException {
+ Visit visit = new Visit();
+ visit.setId(rs.getInt("visits.id"));
+ Date visitDate = rs.getDate("visit_date");
+ visit.setDate(new DateTime(visitDate));
+ visit.setDescription(rs.getString("description"));
+ return visit;
+ }
+}