Skip to content

Commit

Permalink
Fix possible null dereferences in repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
glts committed Apr 2, 2016
1 parent 4518957 commit b6b5c1b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b6b5c1b

Please sign in to comment.