mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-18 05:25:50 +00:00
Merge 66227140aa
into e0be3a39b6
This commit is contained in:
commit
02acc91e6f
33 changed files with 117 additions and 169 deletions
|
@ -32,7 +32,6 @@ public class BaseEntity {
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
protected Integer id;
|
protected Integer id;
|
||||||
|
|
||||||
|
|
||||||
public void setId(Integer id) {
|
public void setId(Integer id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,6 @@ package org.springframework.samples.petclinic.model;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.MappedSuperclass;
|
import javax.persistence.MappedSuperclass;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple JavaBean domain object adds a name property to <code>BaseEntity</code>. Used as a base class for objects
|
* Simple JavaBean domain object adds a name property to <code>BaseEntity</code>. Used as a base class for objects
|
||||||
* needing these properties.
|
* needing these properties.
|
||||||
|
@ -32,7 +31,6 @@ public class NamedEntity extends BaseEntity {
|
||||||
@Column(name = "name")
|
@Column(name = "name")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
|
||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,6 @@ public class Owner extends Person {
|
||||||
@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
|
@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
|
||||||
private Set<Pet> pets;
|
private Set<Pet> pets;
|
||||||
|
|
||||||
|
|
||||||
public String getAddress() {
|
public String getAddress() {
|
||||||
return this.address;
|
return this.address;
|
||||||
}
|
}
|
||||||
|
@ -141,13 +140,13 @@ public class Owner extends Person {
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return new ToStringCreator(this)
|
return new ToStringCreator(this)
|
||||||
|
|
||||||
.append("id", this.getId())
|
.append("id", this.getId())
|
||||||
.append("new", this.isNew())
|
.append("new", this.isNew())
|
||||||
.append("lastName", this.getLastName())
|
.append("lastName", this.getLastName())
|
||||||
.append("firstName", this.getFirstName())
|
.append("firstName", this.getFirstName())
|
||||||
.append("address", this.address)
|
.append("address", this.address)
|
||||||
.append("city", this.city)
|
.append("city", this.city)
|
||||||
.append("telephone", this.telephone)
|
.append("telephone", this.telephone)
|
||||||
.toString();
|
.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,5 +52,4 @@ public class Person extends BaseEntity {
|
||||||
this.lastName = lastName;
|
this.lastName = lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,6 @@ public class Pet extends NamedEntity {
|
||||||
@OneToMany(cascade = CascadeType.ALL, mappedBy = "pet", fetch = FetchType.EAGER)
|
@OneToMany(cascade = CascadeType.ALL, mappedBy = "pet", fetch = FetchType.EAGER)
|
||||||
private Set<Visit> visits;
|
private Set<Visit> visits;
|
||||||
|
|
||||||
|
|
||||||
public void setBirthDate(DateTime birthDate) {
|
public void setBirthDate(DateTime birthDate) {
|
||||||
this.birthDate = birthDate;
|
this.birthDate = birthDate;
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,10 +46,9 @@ public class Vet extends Person {
|
||||||
|
|
||||||
@ManyToMany(fetch = FetchType.EAGER)
|
@ManyToMany(fetch = FetchType.EAGER)
|
||||||
@JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"),
|
@JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"),
|
||||||
inverseJoinColumns = @JoinColumn(name = "specialty_id"))
|
inverseJoinColumns = @JoinColumn(name = "specialty_id"))
|
||||||
private Set<Specialty> specialties;
|
private Set<Specialty> specialties;
|
||||||
|
|
||||||
|
|
||||||
protected void setSpecialtiesInternal(Set<Specialty> specialties) {
|
protected void setSpecialtiesInternal(Set<Specialty> specialties) {
|
||||||
this.specialties = specialties;
|
this.specialties = specialties;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
/*
|
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2013 the original author or authors.
|
||||||
*
|
*
|
||||||
|
|
|
@ -57,7 +57,6 @@ public class Visit extends BaseEntity {
|
||||||
@JoinColumn(name = "pet_id")
|
@JoinColumn(name = "pet_id")
|
||||||
private Pet pet;
|
private Pet pet;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance of Visit for the current date
|
* Creates a new instance of Visit for the current date
|
||||||
*/
|
*/
|
||||||
|
@ -65,7 +64,6 @@ public class Visit extends BaseEntity {
|
||||||
this.date = new DateTime();
|
this.date = new DateTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Getter for property date.
|
* Getter for property date.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,18 +1,3 @@
|
||||||
/*
|
|
||||||
* Copyright 2002-2013 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.
|
|
||||||
*/
|
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2013 the original author or authors.
|
||||||
*
|
*
|
||||||
|
|
|
@ -38,5 +38,4 @@ public interface VetRepository {
|
||||||
*/
|
*/
|
||||||
Collection<Vet> findAll() throws DataAccessException;
|
Collection<Vet> findAll() throws DataAccessException;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,14 +58,13 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
||||||
public JdbcOwnerRepositoryImpl(DataSource dataSource, NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
|
public JdbcOwnerRepositoryImpl(DataSource dataSource, NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
|
||||||
|
|
||||||
this.insertOwner = new SimpleJdbcInsert(dataSource)
|
this.insertOwner = new SimpleJdbcInsert(dataSource)
|
||||||
.withTableName("owners")
|
.withTableName("owners")
|
||||||
.usingGeneratedKeyColumns("id");
|
.usingGeneratedKeyColumns("id");
|
||||||
|
|
||||||
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads {@link Owner Owners} from the data store by last name, returning all owners whose last name <i>starts</i> with
|
* Loads {@link Owner Owners} from the data store by last name, returning all owners whose last name <i>starts</i> with
|
||||||
* the given name; also loads the {@link Pet Pets} and {@link Visit Visits} for the corresponding owners, if not
|
* the given name; also loads the {@link Pet Pets} and {@link Visit Visits} for the corresponding owners, if not
|
||||||
|
@ -76,10 +75,10 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
||||||
Map<String, Object> params = new HashMap<>();
|
Map<String, Object> params = new HashMap<>();
|
||||||
params.put("lastName", lastName + "%");
|
params.put("lastName", lastName + "%");
|
||||||
List<Owner> owners = this.namedParameterJdbcTemplate.query(
|
List<Owner> owners = this.namedParameterJdbcTemplate.query(
|
||||||
"SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE last_name like :lastName",
|
"SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE last_name like :lastName",
|
||||||
params,
|
params,
|
||||||
BeanPropertyRowMapper.newInstance(Owner.class)
|
BeanPropertyRowMapper.newInstance(Owner.class)
|
||||||
);
|
);
|
||||||
loadOwnersPetsAndVisits(owners);
|
loadOwnersPetsAndVisits(owners);
|
||||||
return owners;
|
return owners;
|
||||||
}
|
}
|
||||||
|
@ -95,10 +94,10 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
||||||
Map<String, Object> params = new HashMap<>();
|
Map<String, Object> params = new HashMap<>();
|
||||||
params.put("id", id);
|
params.put("id", id);
|
||||||
owner = this.namedParameterJdbcTemplate.queryForObject(
|
owner = this.namedParameterJdbcTemplate.queryForObject(
|
||||||
"SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id= :id",
|
"SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id= :id",
|
||||||
params,
|
params,
|
||||||
BeanPropertyRowMapper.newInstance(Owner.class)
|
BeanPropertyRowMapper.newInstance(Owner.class)
|
||||||
);
|
);
|
||||||
} catch (EmptyResultDataAccessException ex) {
|
} catch (EmptyResultDataAccessException ex) {
|
||||||
throw new ObjectRetrievalFailureException(Owner.class, id);
|
throw new ObjectRetrievalFailureException(Owner.class, id);
|
||||||
}
|
}
|
||||||
|
@ -109,11 +108,13 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
||||||
public void loadPetsAndVisits(final Owner owner) {
|
public void loadPetsAndVisits(final Owner owner) {
|
||||||
Map<String, Object> params = new HashMap<>();
|
Map<String, Object> params = new HashMap<>();
|
||||||
params.put("id", owner.getId());
|
params.put("id", owner.getId());
|
||||||
final List<JdbcPet> pets = this.namedParameterJdbcTemplate.query(
|
final List<JdbcPet> pets =
|
||||||
"SELECT pets.id, name, birth_date, type_id, owner_id, visits.id as visit_id, visit_date, description, pet_id FROM pets LEFT OUTER JOIN visits ON pets.id = pet_id WHERE owner_id=:id",
|
this.namedParameterJdbcTemplate
|
||||||
params,
|
.query(
|
||||||
new JdbcPetVisitExtractor()
|
"SELECT pets.id, name, birth_date, type_id, owner_id, visits.id as visit_id, visit_date, description, pet_id FROM pets LEFT OUTER JOIN visits ON pets.id = pet_id WHERE owner_id=:id",
|
||||||
);
|
params,
|
||||||
|
new JdbcPetVisitExtractor()
|
||||||
|
);
|
||||||
for (JdbcPet pet : pets) {
|
for (JdbcPet pet : pets) {
|
||||||
owner.addPet(pet);
|
owner.addPet(pet);
|
||||||
}
|
}
|
||||||
|
@ -127,16 +128,16 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
||||||
owner.setId(newKey.intValue());
|
owner.setId(newKey.intValue());
|
||||||
} else {
|
} else {
|
||||||
this.namedParameterJdbcTemplate.update(
|
this.namedParameterJdbcTemplate.update(
|
||||||
"UPDATE owners SET first_name=:firstName, last_name=:lastName, address=:address, " +
|
"UPDATE owners SET first_name=:firstName, last_name=:lastName, address=:address, " +
|
||||||
"city=:city, telephone=:telephone WHERE id=:id",
|
"city=:city, telephone=:telephone WHERE id=:id",
|
||||||
parameterSource);
|
parameterSource);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<PetType> getPetTypes() throws DataAccessException {
|
public Collection<PetType> getPetTypes() throws DataAccessException {
|
||||||
return this.namedParameterJdbcTemplate.query(
|
return this.namedParameterJdbcTemplate.query(
|
||||||
"SELECT id, name FROM types ORDER BY name", new HashMap<String, Object>(),
|
"SELECT id, name FROM types ORDER BY name", new HashMap<String, Object>(),
|
||||||
BeanPropertyRowMapper.newInstance(PetType.class));
|
BeanPropertyRowMapper.newInstance(PetType.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -151,5 +152,4 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,6 @@ class JdbcPet extends Pet {
|
||||||
|
|
||||||
private int ownerId;
|
private int ownerId;
|
||||||
|
|
||||||
|
|
||||||
public void setTypeId(int typeId) {
|
public void setTypeId(int typeId) {
|
||||||
this.typeId = typeId;
|
this.typeId = typeId;
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,14 +58,13 @@ public class JdbcPetRepositoryImpl implements PetRepository {
|
||||||
|
|
||||||
private VisitRepository visitRepository;
|
private VisitRepository visitRepository;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public JdbcPetRepositoryImpl(DataSource dataSource, OwnerRepository ownerRepository, VisitRepository visitRepository) {
|
public JdbcPetRepositoryImpl(DataSource dataSource, OwnerRepository ownerRepository, VisitRepository visitRepository) {
|
||||||
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
||||||
|
|
||||||
this.insertPet = new SimpleJdbcInsert(dataSource)
|
this.insertPet = new SimpleJdbcInsert(dataSource)
|
||||||
.withTableName("pets")
|
.withTableName("pets")
|
||||||
.usingGeneratedKeyColumns("id");
|
.usingGeneratedKeyColumns("id");
|
||||||
|
|
||||||
this.ownerRepository = ownerRepository;
|
this.ownerRepository = ownerRepository;
|
||||||
this.visitRepository = visitRepository;
|
this.visitRepository = visitRepository;
|
||||||
|
@ -75,9 +74,9 @@ public class JdbcPetRepositoryImpl implements PetRepository {
|
||||||
public List<PetType> findPetTypes() throws DataAccessException {
|
public List<PetType> findPetTypes() throws DataAccessException {
|
||||||
Map<String, Object> params = new HashMap<>();
|
Map<String, Object> params = new HashMap<>();
|
||||||
return this.namedParameterJdbcTemplate.query(
|
return this.namedParameterJdbcTemplate.query(
|
||||||
"SELECT id, name FROM types ORDER BY name",
|
"SELECT id, name FROM types ORDER BY name",
|
||||||
params,
|
params,
|
||||||
BeanPropertyRowMapper.newInstance(PetType.class));
|
BeanPropertyRowMapper.newInstance(PetType.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -87,9 +86,9 @@ public class JdbcPetRepositoryImpl implements PetRepository {
|
||||||
Map<String, Object> params = new HashMap<>();
|
Map<String, Object> params = new HashMap<>();
|
||||||
params.put("id", id);
|
params.put("id", id);
|
||||||
pet = this.namedParameterJdbcTemplate.queryForObject(
|
pet = this.namedParameterJdbcTemplate.queryForObject(
|
||||||
"SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id",
|
"SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id",
|
||||||
params,
|
params,
|
||||||
new JdbcPetRowMapper());
|
new JdbcPetRowMapper());
|
||||||
} catch (EmptyResultDataAccessException ex) {
|
} catch (EmptyResultDataAccessException ex) {
|
||||||
throw new ObjectRetrievalFailureException(Pet.class, id);
|
throw new ObjectRetrievalFailureException(Pet.class, id);
|
||||||
}
|
}
|
||||||
|
@ -108,13 +107,13 @@ public class JdbcPetRepositoryImpl implements PetRepository {
|
||||||
public void save(Pet pet) throws DataAccessException {
|
public void save(Pet pet) throws DataAccessException {
|
||||||
if (pet.isNew()) {
|
if (pet.isNew()) {
|
||||||
Number newKey = this.insertPet.executeAndReturnKey(
|
Number newKey = this.insertPet.executeAndReturnKey(
|
||||||
createPetParameterSource(pet));
|
createPetParameterSource(pet));
|
||||||
pet.setId(newKey.intValue());
|
pet.setId(newKey.intValue());
|
||||||
} else {
|
} else {
|
||||||
this.namedParameterJdbcTemplate.update(
|
this.namedParameterJdbcTemplate.update(
|
||||||
"UPDATE pets SET name=:name, birth_date=:birth_date, type_id=:type_id, " +
|
"UPDATE pets SET name=:name, birth_date=:birth_date, type_id=:type_id, " +
|
||||||
"owner_id=:owner_id WHERE id=:id",
|
"owner_id=:owner_id WHERE id=:id",
|
||||||
createPetParameterSource(pet));
|
createPetParameterSource(pet));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,11 +122,11 @@ public class JdbcPetRepositoryImpl implements PetRepository {
|
||||||
*/
|
*/
|
||||||
private MapSqlParameterSource createPetParameterSource(Pet pet) {
|
private MapSqlParameterSource createPetParameterSource(Pet pet) {
|
||||||
return new MapSqlParameterSource()
|
return new MapSqlParameterSource()
|
||||||
.addValue("id", pet.getId())
|
.addValue("id", pet.getId())
|
||||||
.addValue("name", pet.getName())
|
.addValue("name", pet.getName())
|
||||||
.addValue("birth_date", pet.getBirthDate().toDate())
|
.addValue("birth_date", pet.getBirthDate().toDate())
|
||||||
.addValue("type_id", pet.getType().getId())
|
.addValue("type_id", pet.getType().getId())
|
||||||
.addValue("owner_id", pet.getOwner().getId());
|
.addValue("owner_id", pet.getOwner().getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ import java.sql.SQLException;
|
||||||
* {@link OneToManyResultSetExtractor} of Spring Data Core JDBC Extensions.
|
* {@link OneToManyResultSetExtractor} of Spring Data Core JDBC Extensions.
|
||||||
*/
|
*/
|
||||||
public class JdbcPetVisitExtractor extends
|
public class JdbcPetVisitExtractor extends
|
||||||
OneToManyResultSetExtractor<JdbcPet, Visit, Integer> {
|
OneToManyResultSetExtractor<JdbcPet, Visit, Integer> {
|
||||||
|
|
||||||
public JdbcPetVisitExtractor() {
|
public JdbcPetVisitExtractor() {
|
||||||
super(new JdbcPetRowMapper(), new JdbcVisitRowMapper());
|
super(new JdbcPetRowMapper(), new JdbcVisitRowMapper());
|
||||||
|
|
|
@ -60,25 +60,25 @@ public class JdbcVetRepositoryImpl implements VetRepository {
|
||||||
List<Vet> vets = new ArrayList<>();
|
List<Vet> vets = new ArrayList<>();
|
||||||
// Retrieve the list of all vets.
|
// Retrieve the list of all vets.
|
||||||
vets.addAll(this.jdbcTemplate.query(
|
vets.addAll(this.jdbcTemplate.query(
|
||||||
"SELECT id, first_name, last_name FROM vets ORDER BY last_name,first_name",
|
"SELECT id, first_name, last_name FROM vets ORDER BY last_name,first_name",
|
||||||
BeanPropertyRowMapper.newInstance(Vet.class)));
|
BeanPropertyRowMapper.newInstance(Vet.class)));
|
||||||
|
|
||||||
// Retrieve the list of all possible specialties.
|
// Retrieve the list of all possible specialties.
|
||||||
final List<Specialty> specialties = this.jdbcTemplate.query(
|
final List<Specialty> specialties = this.jdbcTemplate.query(
|
||||||
"SELECT id, name FROM specialties",
|
"SELECT id, name FROM specialties",
|
||||||
BeanPropertyRowMapper.newInstance(Specialty.class));
|
BeanPropertyRowMapper.newInstance(Specialty.class));
|
||||||
|
|
||||||
// Build each vet's list of specialties.
|
// Build each vet's list of specialties.
|
||||||
for (Vet vet : vets) {
|
for (Vet vet : vets) {
|
||||||
final List<Integer> vetSpecialtiesIds = this.jdbcTemplate.query(
|
final List<Integer> vetSpecialtiesIds = this.jdbcTemplate.query(
|
||||||
"SELECT specialty_id FROM vet_specialties WHERE vet_id=?",
|
"SELECT specialty_id FROM vet_specialties WHERE vet_id=?",
|
||||||
new BeanPropertyRowMapper<Integer>() {
|
new BeanPropertyRowMapper<Integer>() {
|
||||||
@Override
|
@Override
|
||||||
public Integer mapRow(ResultSet rs, int row) throws SQLException {
|
public Integer mapRow(ResultSet rs, int row) throws SQLException {
|
||||||
return rs.getInt(1);
|
return rs.getInt(1);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
vet.getId());
|
vet.getId());
|
||||||
for (int specialtyId : vetSpecialtiesIds) {
|
for (int specialtyId : vetSpecialtiesIds) {
|
||||||
Specialty specialty = EntityUtils.getById(specialties, Specialty.class, specialtyId);
|
Specialty specialty = EntityUtils.getById(specialties, Specialty.class, specialtyId);
|
||||||
vet.addSpecialty(specialty);
|
vet.addSpecialty(specialty);
|
||||||
|
|
|
@ -50,39 +50,37 @@ public class JdbcVisitRepositoryImpl implements VisitRepository {
|
||||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
|
|
||||||
this.insertVisit = new SimpleJdbcInsert(dataSource)
|
this.insertVisit = new SimpleJdbcInsert(dataSource)
|
||||||
.withTableName("visits")
|
.withTableName("visits")
|
||||||
.usingGeneratedKeyColumns("id");
|
.usingGeneratedKeyColumns("id");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void save(Visit visit) throws DataAccessException {
|
public void save(Visit visit) throws DataAccessException {
|
||||||
if (visit.isNew()) {
|
if (visit.isNew()) {
|
||||||
Number newKey = this.insertVisit.executeAndReturnKey(
|
Number newKey = this.insertVisit.executeAndReturnKey(
|
||||||
createVisitParameterSource(visit));
|
createVisitParameterSource(visit));
|
||||||
visit.setId(newKey.intValue());
|
visit.setId(newKey.intValue());
|
||||||
} else {
|
} else {
|
||||||
throw new UnsupportedOperationException("Visit update not supported");
|
throw new UnsupportedOperationException("Visit update not supported");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a {@link MapSqlParameterSource} based on data values from the supplied {@link Visit} instance.
|
* Creates a {@link MapSqlParameterSource} based on data values from the supplied {@link Visit} instance.
|
||||||
*/
|
*/
|
||||||
private MapSqlParameterSource createVisitParameterSource(Visit visit) {
|
private MapSqlParameterSource createVisitParameterSource(Visit visit) {
|
||||||
return new MapSqlParameterSource()
|
return new MapSqlParameterSource()
|
||||||
.addValue("id", visit.getId())
|
.addValue("id", visit.getId())
|
||||||
.addValue("visit_date", visit.getDate().toDate())
|
.addValue("visit_date", visit.getDate().toDate())
|
||||||
.addValue("description", visit.getDescription())
|
.addValue("description", visit.getDescription())
|
||||||
.addValue("pet_id", visit.getPet().getId());
|
.addValue("pet_id", visit.getPet().getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Visit> findByPetId(Integer petId) {
|
public List<Visit> findByPetId(Integer petId) {
|
||||||
return this.jdbcTemplate.query(
|
return this.jdbcTemplate.query(
|
||||||
"SELECT id as visit_id, visit_date, description FROM visits WHERE pet_id=?",
|
"SELECT id as visit_id, visit_date, description FROM visits WHERE pet_id=?",
|
||||||
new JdbcVisitRowMapper(), petId);
|
new JdbcVisitRowMapper(), petId);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,6 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.repository.jdbc;
|
package org.springframework.samples.petclinic.repository.jdbc;
|
||||||
|
|
||||||
|
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
import org.springframework.jdbc.core.RowMapper;
|
import org.springframework.jdbc.core.RowMapper;
|
||||||
import org.springframework.samples.petclinic.model.Visit;
|
import org.springframework.samples.petclinic.model.Visit;
|
||||||
|
|
|
@ -41,7 +41,6 @@ public class JpaOwnerRepositoryImpl implements OwnerRepository {
|
||||||
@PersistenceContext
|
@PersistenceContext
|
||||||
private EntityManager em;
|
private EntityManager em;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Important: in the current version of this method, we load Owners with all their Pets and Visits while
|
* Important: in the current version of this method, we load Owners with all their Pets and Visits while
|
||||||
* we do not need Visits at all and we only need one property from the Pet objects (the 'name' property).
|
* we do not need Visits at all and we only need one property from the Pet objects (the 'name' property).
|
||||||
|
@ -67,15 +66,14 @@ public class JpaOwnerRepositoryImpl implements OwnerRepository {
|
||||||
return (Owner) query.getSingleResult();
|
return (Owner) query.getSingleResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void save(Owner owner) {
|
public void save(Owner owner) {
|
||||||
if (owner.getId() == null) {
|
if (owner.getId() == null) {
|
||||||
this.em.persist(owner);
|
this.em.persist(owner);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.em.merge(owner);
|
this.em.merge(owner);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,12 +53,12 @@ public class JpaPetRepositoryImpl implements PetRepository {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void save(Pet pet) {
|
public void save(Pet pet) {
|
||||||
if (pet.getId() == null) {
|
if (pet.getId() == null) {
|
||||||
this.em.persist(pet);
|
this.em.persist(pet);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.em.merge(pet);
|
this.em.merge(pet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,6 @@ public class JpaVetRepositoryImpl implements VetRepository {
|
||||||
@PersistenceContext
|
@PersistenceContext
|
||||||
private EntityManager em;
|
private EntityManager em;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(value = "vets")
|
@Cacheable(value = "vets")
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
|
|
|
@ -42,18 +42,16 @@ public class JpaVisitRepositoryImpl implements VisitRepository {
|
||||||
@PersistenceContext
|
@PersistenceContext
|
||||||
private EntityManager em;
|
private EntityManager em;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void save(Visit visit) {
|
public void save(Visit visit) {
|
||||||
if (visit.getId() == null) {
|
if (visit.getId() == null) {
|
||||||
this.em.persist(visit);
|
this.em.persist(visit);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.em.merge(visit);
|
this.em.merge(visit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public List<Visit> findByPetId(Integer petId) {
|
public List<Visit> findByPetId(Integer petId) {
|
||||||
|
|
|
@ -31,11 +31,11 @@ import org.springframework.samples.petclinic.repository.OwnerRepository;
|
||||||
*/
|
*/
|
||||||
public interface SpringDataOwnerRepository extends OwnerRepository, Repository<Owner, Integer> {
|
public interface SpringDataOwnerRepository extends OwnerRepository, Repository<Owner, Integer> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName%")
|
@Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName%")
|
||||||
public Collection<Owner> findByLastName(@Param("lastName") String lastName);
|
public Collection<Owner> findByLastName(@Param("lastName") String lastName);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Query("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id")
|
@Query("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id")
|
||||||
public Owner findById(@Param("id") int id);
|
public Owner findById(@Param("id") int id);
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,6 @@ import org.springframework.samples.petclinic.model.PetType;
|
||||||
import org.springframework.samples.petclinic.model.Vet;
|
import org.springframework.samples.petclinic.model.Vet;
|
||||||
import org.springframework.samples.petclinic.model.Visit;
|
import org.springframework.samples.petclinic.model.Visit;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mostly used as a facade so all controllers have a single point of entry
|
* Mostly used as a facade so all controllers have a single point of entry
|
||||||
*
|
*
|
||||||
|
|
|
@ -78,14 +78,12 @@ public class ClinicServiceImpl implements ClinicService {
|
||||||
ownerRepository.save(owner);
|
ownerRepository.save(owner);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public void saveVisit(Visit visit) throws DataAccessException {
|
public void saveVisit(Visit visit) throws DataAccessException {
|
||||||
visitRepository.save(visit);
|
visitRepository.save(visit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Pet findPetById(int id) throws DataAccessException {
|
public Pet findPetById(int id) throws DataAccessException {
|
||||||
|
@ -105,5 +103,4 @@ public class ClinicServiceImpl implements ClinicService {
|
||||||
return vetRepository.findAll();
|
return vetRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,10 +42,9 @@ public class CallMonitoringAspect {
|
||||||
|
|
||||||
private long accumulatedCallTime = 0;
|
private long accumulatedCallTime = 0;
|
||||||
|
|
||||||
|
|
||||||
@ManagedAttribute
|
@ManagedAttribute
|
||||||
public void setEnabled(boolean enabled) {
|
public void setEnabled(boolean enabled) {
|
||||||
this.enabled = enabled;
|
this.enabled = enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ManagedAttribute
|
@ManagedAttribute
|
||||||
|
@ -66,13 +65,12 @@ public class CallMonitoringAspect {
|
||||||
|
|
||||||
@ManagedAttribute
|
@ManagedAttribute
|
||||||
public long getCallTime() {
|
public long getCallTime() {
|
||||||
if (this.callCount > 0)
|
if (this.callCount > 0)
|
||||||
return this.accumulatedCallTime / this.callCount;
|
return this.accumulatedCallTime / this.callCount;
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Around("within(@org.springframework.stereotype.Repository *)")
|
@Around("within(@org.springframework.stereotype.Repository *)")
|
||||||
public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
|
public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||||
if (this.enabled) {
|
if (this.enabled) {
|
||||||
|
|
|
@ -43,7 +43,7 @@ public abstract class EntityUtils {
|
||||||
* if the entity was not found
|
* if the entity was not found
|
||||||
*/
|
*/
|
||||||
public static <T extends BaseEntity> T getById(Collection<T> entities, Class<T> entityClass, int entityId)
|
public static <T extends BaseEntity> T getById(Collection<T> entities, Class<T> entityClass, int entityId)
|
||||||
throws ObjectRetrievalFailureException {
|
throws ObjectRetrievalFailureException {
|
||||||
for (T entity : entities) {
|
for (T entity : entities) {
|
||||||
if (entity.getId() == entityId && entityClass.isInstance(entity)) {
|
if (entity.getId() == entityId && entityClass.isInstance(entity)) {
|
||||||
return entity;
|
return entity;
|
||||||
|
|
|
@ -33,8 +33,7 @@ public class CrashController {
|
||||||
@RequestMapping(value = "/oups", method = RequestMethod.GET)
|
@RequestMapping(value = "/oups", method = RequestMethod.GET)
|
||||||
public String triggerException() {
|
public String triggerException() {
|
||||||
throw new RuntimeException("Expected: controller used to showcase what " +
|
throw new RuntimeException("Expected: controller used to showcase what " +
|
||||||
"happens when an exception is thrown");
|
"happens when an exception is thrown");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,6 @@ public class OwnerController {
|
||||||
|
|
||||||
private final ClinicService clinicService;
|
private final ClinicService clinicService;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public OwnerController(ClinicService clinicService) {
|
public OwnerController(ClinicService clinicService) {
|
||||||
this.clinicService = clinicService;
|
this.clinicService = clinicService;
|
||||||
|
@ -98,11 +97,10 @@ public class OwnerController {
|
||||||
return "owners/findOwners";
|
return "owners/findOwners";
|
||||||
}
|
}
|
||||||
else if (results.size() == 1) {
|
else if (results.size() == 1) {
|
||||||
// 1 owner found
|
// 1 owner found
|
||||||
owner = results.iterator().next();
|
owner = results.iterator().next();
|
||||||
return "redirect:/owners/" + owner.getId();
|
return "redirect:/owners/" + owner.getId();
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// multiple owners found
|
// multiple owners found
|
||||||
model.put("selections", results);
|
model.put("selections", results);
|
||||||
return "owners/ownersList";
|
return "owners/ownersList";
|
||||||
|
|
|
@ -47,7 +47,6 @@ public class PetController {
|
||||||
|
|
||||||
private final ClinicService clinicService;
|
private final ClinicService clinicService;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public PetController(ClinicService clinicService) {
|
public PetController(ClinicService clinicService) {
|
||||||
this.clinicService = clinicService;
|
this.clinicService = clinicService;
|
||||||
|
|
|
@ -15,7 +15,6 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.web;
|
package org.springframework.samples.petclinic.web;
|
||||||
|
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
@ -41,7 +40,6 @@ public class PetTypeFormatter implements Formatter<PetType> {
|
||||||
|
|
||||||
private final ClinicService clinicService;
|
private final ClinicService clinicService;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public PetTypeFormatter(ClinicService clinicService) {
|
public PetTypeFormatter(ClinicService clinicService) {
|
||||||
this.clinicService = clinicService;
|
this.clinicService = clinicService;
|
||||||
|
|
|
@ -61,5 +61,4 @@ public class PetValidator implements Validator {
|
||||||
return Pet.class.equals(clazz);
|
return Pet.class.equals(clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,6 @@ public class VetController {
|
||||||
|
|
||||||
private final ClinicService clinicService;
|
private final ClinicService clinicService;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public VetController(ClinicService clinicService) {
|
public VetController(ClinicService clinicService) {
|
||||||
this.clinicService = clinicService;
|
this.clinicService = clinicService;
|
||||||
|
@ -60,5 +59,4 @@ public class VetController {
|
||||||
return vets;
|
return vets;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,6 @@ public class VisitController {
|
||||||
|
|
||||||
private final ClinicService clinicService;
|
private final ClinicService clinicService;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public VisitController(ClinicService clinicService) {
|
public VisitController(ClinicService clinicService) {
|
||||||
this.clinicService = clinicService;
|
this.clinicService = clinicService;
|
||||||
|
@ -71,13 +70,13 @@ public class VisitController {
|
||||||
return visit;
|
return visit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is called
|
// Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is called
|
||||||
@RequestMapping(value = "/owners/*/pets/{petId}/visits/new", method = RequestMethod.GET)
|
@RequestMapping(value = "/owners/*/pets/{petId}/visits/new", method = RequestMethod.GET)
|
||||||
public String initNewVisitForm(@PathVariable("petId") int petId, Map<String, Object> model) {
|
public String initNewVisitForm(@PathVariable("petId") int petId, Map<String, Object> model) {
|
||||||
return "pets/createOrUpdateVisitForm";
|
return "pets/createOrUpdateVisitForm";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is called
|
// Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is called
|
||||||
@RequestMapping(value = "/owners/{ownerId}/pets/{petId}/visits/new", method = RequestMethod.POST)
|
@RequestMapping(value = "/owners/{ownerId}/pets/{petId}/visits/new", method = RequestMethod.POST)
|
||||||
public String processNewVisitForm(@Valid Visit visit, BindingResult result) {
|
public String processNewVisitForm(@Valid Visit visit, BindingResult result) {
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
|
|
Loading…
Reference in a new issue