diff --git a/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java index 131833706..841d0bcd8 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java @@ -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. * @@ -33,6 +18,9 @@ package org.springframework.samples.petclinic.repository; import java.util.Collection; import org.springframework.dao.DataAccessException; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.Repository; +import org.springframework.data.repository.query.Param; import org.springframework.samples.petclinic.model.BaseEntity; import org.springframework.samples.petclinic.model.Owner; @@ -45,36 +33,31 @@ import org.springframework.samples.petclinic.model.Owner; * @author Sam Brannen * @author Michael Isvy */ -public interface OwnerRepository { +public interface OwnerRepository extends Repository { /** - * Retrieve Owners from the data store by last name, returning all owners whose last name starts - * with the given name. - * + * Retrieve {@link Owner}s from the data store by last name, returning all owners + * whose last name starts with the given name. * @param lastName Value to search for - * @return a Collection of matching Owners (or an empty Collection if none - * found) + * @return a Collection of matching {@link Owner}s (or an empty Collection if none + * found) */ - Collection findByLastName(String lastName) throws DataAccessException; + @Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName%") + Collection findByLastName(@Param("lastName") String lastName); /** - * Retrieve an Owner from the data store by id. - * + * Retrieve an {@link Owner} from the data store by id. * @param id the id to search for - * @return the Owner if found - * @throws org.springframework.dao.DataRetrievalFailureException - * if not found + * @return the {@link Owner} if found */ - Owner findById(int id) throws DataAccessException; - + @Query("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id") + Owner findById(@Param("id") int id); /** - * Save an Owner to the data store, either inserting or updating it. - * - * @param owner the Owner to save - * @see BaseEntity#isNew + * Save an {@link Owner} to the data store, either inserting or updating it. + * @param owner the {@link Owner} to save */ - void save(Owner owner) throws DataAccessException; + void save(Owner owner); } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java index 693b2e5e9..58e73d7f6 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java @@ -18,6 +18,8 @@ package org.springframework.samples.petclinic.repository; import java.util.List; import org.springframework.dao.DataAccessException; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.Repository; import org.springframework.samples.petclinic.model.BaseEntity; import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.model.PetType; @@ -31,31 +33,27 @@ import org.springframework.samples.petclinic.model.PetType; * @author Sam Brannen * @author Michael Isvy */ -public interface PetRepository { +public interface PetRepository extends Repository { /** - * Retrieve all PetTypes from the data store. - * - * @return a Collection of PetTypes + * Retrieve all {@link PetType}s from the data store. + * @return a Collection of {@link PetType}s. */ - List findPetTypes() throws DataAccessException; + @Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name") + List findPetTypes(); /** - * Retrieve a Pet from the data store by id. - * + * Retrieve a {@link Pet} from the data store by id. * @param id the id to search for - * @return the Pet if found - * @throws org.springframework.dao.DataRetrievalFailureException - * if not found + * @return the {@link Pet} if found */ - Pet findById(int id) throws DataAccessException; + Pet findById(int id); /** - * Save a Pet to the data store, either inserting or updating it. - * - * @param pet the Pet to save - * @see BaseEntity#isNew + * Save a {@link Pet} to the data store, either inserting or updating it. + * @param pet the {@link Pet} to save */ - void save(Pet pet) throws DataAccessException; + void save(Pet pet); } + diff --git a/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java index 923c3c23d..aabdfd65d 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java @@ -18,6 +18,7 @@ package org.springframework.samples.petclinic.repository; import java.util.Collection; import org.springframework.dao.DataAccessException; +import org.springframework.data.repository.Repository; import org.springframework.samples.petclinic.model.Vet; /** @@ -29,7 +30,7 @@ import org.springframework.samples.petclinic.model.Vet; * @author Sam Brannen * @author Michael Isvy */ -public interface VetRepository { +public interface VetRepository extends Repository { /** * Retrieve all Vets from the data store. diff --git a/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java index 96b7a208c..e734f3275 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java @@ -18,6 +18,7 @@ package org.springframework.samples.petclinic.repository; import java.util.List; import org.springframework.dao.DataAccessException; +import org.springframework.data.repository.Repository; import org.springframework.samples.petclinic.model.BaseEntity; import org.springframework.samples.petclinic.model.Visit; @@ -30,7 +31,7 @@ import org.springframework.samples.petclinic.model.Visit; * @author Sam Brannen * @author Michael Isvy */ -public interface VisitRepository { +public interface VisitRepository extends Repository { /** * Save a Visit to the data store, either inserting or updating it. 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 deleted file mode 100644 index 579de5284..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * 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. - */ -package org.springframework.samples.petclinic.repository.jdbc; - -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.sql.DataSource; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.DataAccessException; -import org.springframework.dao.EmptyResultDataAccessException; -import org.springframework.jdbc.core.BeanPropertyRowMapper; -import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; -import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; -import org.springframework.jdbc.core.simple.SimpleJdbcInsert; -import org.springframework.orm.ObjectRetrievalFailureException; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.model.Visit; -import org.springframework.samples.petclinic.repository.OwnerRepository; -import org.springframework.samples.petclinic.repository.VisitRepository; -import org.springframework.samples.petclinic.util.EntityUtils; -import org.springframework.stereotype.Repository; - -/** - * A simple JDBC-based implementation of the {@link OwnerRepository} interface. - * - * @author Ken Krebs - * @author Juergen Hoeller - * @author Rob Harrop - * @author Sam Brannen - * @author Thomas Risberg - * @author Mark Fisher - */ -@Repository -public class JdbcOwnerRepositoryImpl implements OwnerRepository { - - private VisitRepository visitRepository; - - private NamedParameterJdbcTemplate namedParameterJdbcTemplate; - - private SimpleJdbcInsert insertOwner; - - @Autowired - public JdbcOwnerRepositoryImpl(DataSource dataSource, NamedParameterJdbcTemplate namedParameterJdbcTemplate, - VisitRepository visitRepository) { - - this.insertOwner = new SimpleJdbcInsert(dataSource) - .withTableName("owners") - .usingGeneratedKeyColumns("id"); - - this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); - - this.visitRepository = visitRepository; - } - - - /** - * Loads {@link Owner Owners} from the data store by last name, returning all owners whose last name starts with - * the given name; also loads the {@link Pet Pets} and {@link Visit Visits} for the corresponding owners, if not - * already loaded. - */ - @Override - public Collection findByLastName(String lastName) throws DataAccessException { - Map params = new HashMap(); - params.put("lastName", lastName + "%"); - List owners = this.namedParameterJdbcTemplate.query( - "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE last_name like :lastName", - params, - BeanPropertyRowMapper.newInstance(Owner.class) - ); - loadOwnersPetsAndVisits(owners); - return owners; - } - - /** - * Loads the {@link Owner} with the supplied id; also loads the {@link Pet Pets} and {@link Visit Visits} - * for the corresponding owner, if not already loaded. - */ - @Override - public Owner findById(int id) throws DataAccessException { - Owner owner; - try { - Map params = new HashMap(); - params.put("id", id); - owner = this.namedParameterJdbcTemplate.queryForObject( - "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id= :id", - params, - BeanPropertyRowMapper.newInstance(Owner.class) - ); - } catch (EmptyResultDataAccessException ex) { - throw new ObjectRetrievalFailureException(Owner.class, id); - } - loadPetsAndVisits(owner); - return owner; - } - - public void loadPetsAndVisits(final Owner owner) { - 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", - params, - new JdbcPetRowMapper() - ); - 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); - } - } - } - - @Override - public void save(Owner owner) throws DataAccessException { - BeanPropertySqlParameterSource parameterSource = new BeanPropertySqlParameterSource(owner); - if (owner.isNew()) { - Number newKey = this.insertOwner.executeAndReturnKey(parameterSource); - owner.setId(newKey.intValue()); - } else { - this.namedParameterJdbcTemplate.update( - "UPDATE owners SET first_name=:firstName, last_name=:lastName, address=:address, " + - "city=:city, telephone=:telephone WHERE id=:id", - parameterSource); - } - } - - public Collection getPetTypes() throws DataAccessException { - return this.namedParameterJdbcTemplate.query( - "SELECT id, name FROM types ORDER BY name", new HashMap(), - BeanPropertyRowMapper.newInstance(PetType.class)); - } - - /** - * Loads the {@link Pet} and {@link Visit} data for the supplied {@link List} of {@link Owner Owners}. - * - * @param owners the list of owners for whom the pet and visit data should be loaded - * @see #loadPetsAndVisits(Owner) - */ - private void loadOwnersPetsAndVisits(List owners) { - for (Owner owner : owners) { - loadPetsAndVisits(owner); - } - } - - -} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPet.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPet.java deleted file mode 100644 index 39ba53b83..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPet.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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. - */ -package org.springframework.samples.petclinic.repository.jdbc; - -import org.springframework.samples.petclinic.model.Pet; - -/** - * Subclass of Pet that carries temporary id properties which are only relevant for a JDBC implementation of the - * PetRepository. - * - * @author Juergen Hoeller - */ -class JdbcPet extends Pet { - - private int typeId; - - private int ownerId; - - - public void setTypeId(int typeId) { - this.typeId = typeId; - } - - public int getTypeId() { - return this.typeId; - } - - public void setOwnerId(int ownerId) { - this.ownerId = ownerId; - } - - public int getOwnerId() { - return this.ownerId; - } - -} 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 deleted file mode 100644 index 546451dcc..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * 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. - */ -package org.springframework.samples.petclinic.repository.jdbc; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.sql.DataSource; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.DataAccessException; -import org.springframework.dao.EmptyResultDataAccessException; -import org.springframework.jdbc.core.BeanPropertyRowMapper; -import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; -import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; -import org.springframework.jdbc.core.simple.SimpleJdbcInsert; -import org.springframework.orm.ObjectRetrievalFailureException; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.model.Visit; -import org.springframework.samples.petclinic.repository.OwnerRepository; -import org.springframework.samples.petclinic.repository.PetRepository; -import org.springframework.samples.petclinic.repository.VisitRepository; -import org.springframework.samples.petclinic.util.EntityUtils; -import org.springframework.stereotype.Repository; - -/** - * @author Ken Krebs - * @author Juergen Hoeller - * @author Rob Harrop - * @author Sam Brannen - * @author Thomas Risberg - * @author Mark Fisher - */ -@Repository -public class JdbcPetRepositoryImpl implements PetRepository { - - private NamedParameterJdbcTemplate namedParameterJdbcTemplate; - - private SimpleJdbcInsert insertPet; - - private OwnerRepository ownerRepository; - - private VisitRepository visitRepository; - - - @Autowired - public JdbcPetRepositoryImpl(DataSource dataSource, OwnerRepository ownerRepository, VisitRepository visitRepository) { - this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); - - this.insertPet = new SimpleJdbcInsert(dataSource) - .withTableName("pets") - .usingGeneratedKeyColumns("id"); - - this.ownerRepository = ownerRepository; - this.visitRepository = visitRepository; - } - - @Override - public List findPetTypes() throws DataAccessException { - Map params = new HashMap(); - return this.namedParameterJdbcTemplate.query( - "SELECT id, name FROM types ORDER BY name", - params, - BeanPropertyRowMapper.newInstance(PetType.class)); - } - - @Override - public Pet findById(int id) throws DataAccessException { - JdbcPet pet; - try { - Map params = new HashMap(); - params.put("id", id); - pet = this.namedParameterJdbcTemplate.queryForObject( - "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id", - params, - new JdbcPetRowMapper()); - } catch (EmptyResultDataAccessException ex) { - throw new ObjectRetrievalFailureException(Pet.class, new Integer(id)); - } - Owner owner = this.ownerRepository.findById(pet.getOwnerId()); - owner.addPet(pet); - pet.setType(EntityUtils.getById(findPetTypes(), PetType.class, pet.getTypeId())); - - List visits = this.visitRepository.findByPetId(pet.getId()); - for (Visit visit : visits) { - pet.addVisit(visit); - } - return pet; - } - - @Override - public void save(Pet pet) throws DataAccessException { - if (pet.isNew()) { - Number newKey = this.insertPet.executeAndReturnKey( - createPetParameterSource(pet)); - pet.setId(newKey.intValue()); - } else { - this.namedParameterJdbcTemplate.update( - "UPDATE pets SET name=:name, birth_date=:birth_date, type_id=:type_id, " + - "owner_id=:owner_id WHERE id=:id", - createPetParameterSource(pet)); - } - } - - /** - * Creates a {@link MapSqlParameterSource} based on data values from the supplied {@link Pet} instance. - */ - private MapSqlParameterSource createPetParameterSource(Pet pet) { - 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()); - } - -} 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 deleted file mode 100644 index 4164f746f..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRowMapper.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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. - */ -package org.springframework.samples.petclinic.repository.jdbc; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Date; - -import org.joda.time.DateTime; -import org.springframework.jdbc.core.BeanPropertyRowMapper; - -/** - * {@link BeanPropertyRowMapper} implementation mapping data from a {@link ResultSet} to the corresponding properties - * of the {@link JdbcPet} class. - */ -class JdbcPetRowMapper extends BeanPropertyRowMapper { - - @Override - public JdbcPet mapRow(ResultSet rs, int rownum) throws SQLException { - JdbcPet pet = new JdbcPet(); - pet.setId(rs.getInt("id")); - pet.setName(rs.getString("name")); - Date birthDate = rs.getDate("birth_date"); - pet.setBirthDate(new DateTime(birthDate)); - pet.setTypeId(rs.getInt("type_id")); - pet.setOwnerId(rs.getInt("owner_id")); - return pet; - } -} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java deleted file mode 100644 index 9a85bde11..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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. - */ -package org.springframework.samples.petclinic.repository.jdbc; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -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.samples.petclinic.model.Specialty; -import org.springframework.samples.petclinic.model.Vet; -import org.springframework.samples.petclinic.repository.VetRepository; -import org.springframework.samples.petclinic.util.EntityUtils; -import org.springframework.stereotype.Repository; - -/** - * A simple JDBC-based implementation of the {@link VetRepository} interface. Uses @Cacheable to cache the result of the - * {@link findAll} method - * - * @author Ken Krebs - * @author Juergen Hoeller - * @author Rob Harrop - * @author Sam Brannen - * @author Thomas Risberg - * @author Mark Fisher - * @author Michael Isvy - */ -@Repository -public class JdbcVetRepositoryImpl implements VetRepository { - - private JdbcTemplate jdbcTemplate; - - @Autowired - public JdbcVetRepositoryImpl(JdbcTemplate jdbcTemplate) { - this.jdbcTemplate = jdbcTemplate; - } - - /** - * Refresh the cache of Vets that the ClinicService is holding. - * - * @see org.springframework.samples.petclinic.model.service.ClinicService#shouldFindVets() - */ - @Override - public Collection findAll() throws DataAccessException { - List vets = new ArrayList(); - // Retrieve the list of all vets. - vets.addAll(this.jdbcTemplate.query( - "SELECT id, first_name, last_name FROM vets ORDER BY last_name,first_name", - BeanPropertyRowMapper.newInstance(Vet.class))); - - // Retrieve the list of all possible specialties. - final List specialties = this.jdbcTemplate.query( - "SELECT id, name FROM specialties", - BeanPropertyRowMapper.newInstance(Specialty.class)); - - // Build each vet's list of specialties. - for (Vet vet : vets) { - final List vetSpecialtiesIds = this.jdbcTemplate.query( - "SELECT specialty_id FROM vet_specialties WHERE vet_id=?", - new BeanPropertyRowMapper() { - @Override - public Integer mapRow(ResultSet rs, int row) throws SQLException { - return Integer.valueOf(rs.getInt(1)); - } - }, - vet.getId().intValue()); - for (int specialtyId : vetSpecialtiesIds) { - Specialty specialty = EntityUtils.getById(specialties, Specialty.class, specialtyId); - vet.addSpecialty(specialty); - } - } - return vets; - } -} 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 deleted file mode 100644 index b6a004561..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * 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. - */ -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; -import org.springframework.samples.petclinic.model.Visit; -import org.springframework.samples.petclinic.repository.VisitRepository; -import org.springframework.stereotype.Repository; - -/** - * A simple JDBC-based implementation of the {@link VisitRepository} interface. - * - * @author Ken Krebs - * @author Juergen Hoeller - * @author Rob Harrop - * @author Sam Brannen - * @author Thomas Risberg - * @author Mark Fisher - * @author Michael Isvy - */ -@Repository -public class JdbcVisitRepositoryImpl implements VisitRepository { - - private JdbcTemplate jdbcTemplate; - - private SimpleJdbcInsert insertVisit; - - @Autowired - public JdbcVisitRepositoryImpl(DataSource dataSource) { - this.jdbcTemplate = new JdbcTemplate(dataSource); - - this.insertVisit = new SimpleJdbcInsert(dataSource) - .withTableName("visits") - .usingGeneratedKeyColumns("id"); - } - - - @Override - public void save(Visit visit) throws DataAccessException { - if (visit.isNew()) { - Number newKey = this.insertVisit.executeAndReturnKey( - createVisitParameterSource(visit)); - visit.setId(newKey.intValue()); - } else { - throw new UnsupportedOperationException("Visit update not supported"); - } - } - - public void deletePet(int id) throws DataAccessException { - this.jdbcTemplate.update("DELETE FROM pets WHERE id=?", id); - } - - - /** - * Creates a {@link MapSqlParameterSource} based on data values from the supplied {@link Visit} instance. - */ - private MapSqlParameterSource createVisitParameterSource(Visit visit) { - return new MapSqlParameterSource() - .addValue("id", visit.getId()) - .addValue("visit_date", visit.getDate().toDate()) - .addValue("description", visit.getDescription()) - .addValue("pet_id", visit.getPet().getId()); - } - - @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; - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/package-info.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/package-info.java deleted file mode 100644 index edd0bf855..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/package-info.java +++ /dev/null @@ -1,9 +0,0 @@ - -/** - * - * The classes in this package represent the JDBC implementation - * of PetClinic's persistence layer. - * - */ -package org.springframework.samples.petclinic.repository.jdbc; - diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaOwnerRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaOwnerRepositoryImpl.java deleted file mode 100644 index c7398df5b..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaOwnerRepositoryImpl.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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. - */ -package org.springframework.samples.petclinic.repository.jpa; - -import java.util.Collection; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.persistence.Query; - -import org.springframework.orm.hibernate3.support.OpenSessionInViewFilter; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.repository.OwnerRepository; -import org.springframework.stereotype.Repository; - -/** - * JPA implementation of the {@link OwnerRepository} interface. - * - * @author Mike Keith - * @author Rod Johnson - * @author Sam Brannen - * @author Michael Isvy - * @since 22.4.2006 - */ -@Repository -public class JpaOwnerRepositoryImpl implements OwnerRepository { - - @PersistenceContext - private EntityManager em; - - - /** - * 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). - * There are some ways to improve it such as: - * - creating a Ligtweight class (example here: https://community.jboss.org/wiki/LightweightClass) - * - Turning on lazy-loading and using {@link OpenSessionInViewFilter} - */ - @SuppressWarnings("unchecked") - public Collection findByLastName(String lastName) { - // using 'join fetch' because a single query should load both owners and pets - // using 'left join fetch' because it might happen that an owner does not have pets yet - Query query = this.em.createQuery("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName"); - query.setParameter("lastName", lastName + "%"); - return query.getResultList(); - } - - @Override - public Owner findById(int id) { - // using 'join fetch' because a single query should load both owners and pets - // using 'left join fetch' because it might happen that an owner does not have pets yet - Query query = this.em.createQuery("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id"); - query.setParameter("id", id); - return (Owner) query.getSingleResult(); - } - - - @Override - public void save(Owner owner) { - if (owner.getId() == null) { - this.em.persist(owner); - } - else { - this.em.merge(owner); - } - - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryImpl.java deleted file mode 100644 index 84d564da4..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryImpl.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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. - */ -package org.springframework.samples.petclinic.repository.jpa; - -import java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; - -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.repository.PetRepository; -import org.springframework.stereotype.Repository; - -/** - * JPA implementation of the {@link PetRepository} interface. - * - * @author Mike Keith - * @author Rod Johnson - * @author Sam Brannen - * @author Michael Isvy - * @since 22.4.2006 - */ -@Repository -public class JpaPetRepositoryImpl implements PetRepository { - - @PersistenceContext - private EntityManager em; - - @Override - @SuppressWarnings("unchecked") - public List findPetTypes() { - return this.em.createQuery("SELECT ptype FROM PetType ptype ORDER BY ptype.name").getResultList(); - } - - @Override - public Pet findById(int id) { - return this.em.find(Pet.class, id); - } - - @Override - public void save(Pet pet) { - if (pet.getId() == null) { - this.em.persist(pet); - } - else { - this.em.merge(pet); - } - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVetRepositoryImpl.java deleted file mode 100644 index e4c222b65..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVetRepositoryImpl.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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. - */ -package org.springframework.samples.petclinic.repository.jpa; - -import java.util.Collection; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; - -import org.springframework.cache.annotation.Cacheable; -import org.springframework.samples.petclinic.model.Vet; -import org.springframework.samples.petclinic.repository.VetRepository; -import org.springframework.stereotype.Repository; - -/** - * JPA implementation of the {@link VetRepository} interface. - * - * @author Mike Keith - * @author Rod Johnson - * @author Sam Brannen - * @author Michael Isvy - * @since 22.4.2006 - */ -@Repository -public class JpaVetRepositoryImpl implements VetRepository { - - @PersistenceContext - private EntityManager em; - - - @Override - @Cacheable(value = "vets") - @SuppressWarnings("unchecked") - public Collection findAll() { - return this.em.createQuery("SELECT distinct vet FROM Vet vet left join fetch vet.specialties ORDER BY vet.lastName, vet.firstName").getResultList(); - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryImpl.java deleted file mode 100644 index 3415def96..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryImpl.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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. - */ -package org.springframework.samples.petclinic.repository.jpa; - -import java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.persistence.Query; - -import org.springframework.samples.petclinic.model.Visit; -import org.springframework.samples.petclinic.repository.VisitRepository; -import org.springframework.stereotype.Repository; - -/** - * JPA implementation of the ClinicService interface using EntityManager. - *

- *

The mappings are defined in "orm.xml" located in the META-INF directory. - * - * @author Mike Keith - * @author Rod Johnson - * @author Sam Brannen - * @author Michael Isvy - * @since 22.4.2006 - */ -@Repository -public class JpaVisitRepositoryImpl implements VisitRepository { - - @PersistenceContext - private EntityManager em; - - - @Override - public void save(Visit visit) { - if (visit.getId() == null) { - this.em.persist(visit); - } - else { - this.em.merge(visit); - } - } - - - @Override - @SuppressWarnings("unchecked") - public List findByPetId(Integer petId) { - Query query = this.em.createQuery("SELECT visit FROM Visit v where v.pets.id= :id"); - query.setParameter("id", petId); - return query.getResultList(); - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/package-info.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/package-info.java deleted file mode 100644 index 13c8552ed..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/package-info.java +++ /dev/null @@ -1,9 +0,0 @@ - -/** - * - * The classes in this package represent the JPA implementation - * of PetClinic's persistence layer. - * - */ -package org.springframework.samples.petclinic.repository.jpa; - diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataOwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataOwnerRepository.java deleted file mode 100644 index ca1f709f6..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataOwnerRepository.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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. - */ -package org.springframework.samples.petclinic.repository.springdatajpa; - -import java.util.Collection; - -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.Repository; -import org.springframework.data.repository.query.Param; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.repository.OwnerRepository; - -/** - * Spring Data JPA specialization of the {@link OwnerRepository} interface - * - * @author Michael Isvy - * @since 15.1.2013 - */ -public interface SpringDataOwnerRepository extends OwnerRepository, Repository { - - @Override - @Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName%") - public Collection findByLastName(@Param("lastName") String lastName); - - @Override - @Query("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id") - public Owner findById(@Param("id") int id); -} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetRepository.java deleted file mode 100644 index 56a413147..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetRepository.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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. - */ -package org.springframework.samples.petclinic.repository.springdatajpa; - -import java.util.List; - -import org.springframework.dao.DataAccessException; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.Repository; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.repository.PetRepository; - -/** - * Spring Data JPA specialization of the {@link PetRepository} interface - * - * @author Michael Isvy - * @since 15.1.2013 - */ -public interface SpringDataPetRepository extends PetRepository, Repository { - - @Override - @Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name") - List findPetTypes() throws DataAccessException; -} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVetRepository.java deleted file mode 100644 index b8211b707..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVetRepository.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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. - */ -package org.springframework.samples.petclinic.repository.springdatajpa; - -import org.springframework.data.repository.Repository; -import org.springframework.samples.petclinic.model.Vet; -import org.springframework.samples.petclinic.repository.VetRepository; - -/** - * Spring Data JPA specialization of the {@link VetRepository} interface - * - * @author Michael Isvy - * @since 15.1.2013 - */ -public interface SpringDataVetRepository extends VetRepository, Repository { -} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVisitRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVisitRepository.java deleted file mode 100644 index 84740224b..000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVisitRepository.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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. - */ -package org.springframework.samples.petclinic.repository.springdatajpa; - -import org.springframework.data.repository.Repository; -import org.springframework.samples.petclinic.model.Visit; -import org.springframework.samples.petclinic.repository.VisitRepository; - -/** - * Spring Data JPA specialization of the {@link VisitRepository} interface - * - * @author Michael Isvy - * @since 15.1.2013 - */ -public interface SpringDataVisitRepository extends VisitRepository, Repository { -}