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 77bb221..b010e45 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 @@ -21,6 +21,7 @@ import javax.sql.DataSource; +import org.joda.time.LocalDate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.dao.EmptyResultDataAccessException; @@ -124,12 +125,16 @@ public void save(Pet pet) throws DataAccessException { * Creates a {@link MapSqlParameterSource} based on data values from the supplied {@link Pet} instance. */ private MapSqlParameterSource createPetParameterSource(Pet pet) { + LocalDate birthDate = pet.getBirthDate(); + PetType type = pet.getType(); + Owner owner = pet.getOwner(); + return new MapSqlParameterSource() .addValue("id", pet.getId()) .addValue("name", pet.getName()) - .addValue("birth_date", pet.getBirthDate().toDate()) - .addValue("type_id", pet.getType().getId()) - .addValue("owner_id", pet.getOwner().getId()); + .addValue("birth_date", birthDate == null ? null : birthDate.toDate()) + .addValue("type_id", type == null ? null : type.getId()) + .addValue("owner_id", owner == null ? null : owner.getId()); } } 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 330ca1a..49accd8 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,11 +15,13 @@ */ package org.springframework.samples.petclinic.repository.jdbc; +import org.joda.time.DateTime; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.simple.SimpleJdbcInsert; +import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.model.Visit; import org.springframework.samples.petclinic.repository.VisitRepository; import org.springframework.stereotype.Repository; @@ -71,11 +73,14 @@ public void save(Visit visit) throws DataAccessException { * Creates a {@link MapSqlParameterSource} based on data values from the supplied {@link Visit} instance. */ private MapSqlParameterSource createVisitParameterSource(Visit visit) { + DateTime date = visit.getDate(); + Pet pet = visit.getPet(); + return new MapSqlParameterSource() .addValue("id", visit.getId()) - .addValue("visit_date", visit.getDate().toDate()) + .addValue("visit_date", date == null ? null : date.toDate()) .addValue("description", visit.getDescription()) - .addValue("pet_id", visit.getPet().getId()); + .addValue("pet_id", pet == null ? null : pet.getId()); } @Override