mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 04:55:50 +00:00
migrate to spring-data for repositories
This commit is contained in:
parent
f22bfed274
commit
175cf7dfac
20 changed files with 35 additions and 1062 deletions
|
@ -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.
|
||||||
*
|
*
|
||||||
|
@ -33,6 +18,9 @@ package org.springframework.samples.petclinic.repository;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
import org.springframework.dao.DataAccessException;
|
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.BaseEntity;
|
||||||
import org.springframework.samples.petclinic.model.Owner;
|
import org.springframework.samples.petclinic.model.Owner;
|
||||||
|
|
||||||
|
@ -45,36 +33,31 @@ import org.springframework.samples.petclinic.model.Owner;
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @author Michael Isvy
|
* @author Michael Isvy
|
||||||
*/
|
*/
|
||||||
public interface OwnerRepository {
|
public interface OwnerRepository extends Repository<Owner, Integer> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve <code>Owner</code>s from the data store by last name, returning all owners whose last name <i>starts</i>
|
* Retrieve {@link Owner}s from the data store by last name, returning all owners
|
||||||
* with the given name.
|
* whose last name <i>starts</i> with the given name.
|
||||||
*
|
|
||||||
* @param lastName Value to search for
|
* @param lastName Value to search for
|
||||||
* @return a <code>Collection</code> of matching <code>Owner</code>s (or an empty <code>Collection</code> if none
|
* @return a Collection of matching {@link Owner}s (or an empty Collection if none
|
||||||
* found)
|
* found)
|
||||||
*/
|
*/
|
||||||
Collection<Owner> findByLastName(String lastName) throws DataAccessException;
|
@Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName%")
|
||||||
|
Collection<Owner> findByLastName(@Param("lastName") String lastName);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve an <code>Owner</code> from the data store by id.
|
* Retrieve an {@link Owner} from the data store by id.
|
||||||
*
|
|
||||||
* @param id the id to search for
|
* @param id the id to search for
|
||||||
* @return the <code>Owner</code> if found
|
* @return the {@link Owner} if found
|
||||||
* @throws org.springframework.dao.DataRetrievalFailureException
|
|
||||||
* if not 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 <code>Owner</code> to the data store, either inserting or updating it.
|
* Save an {@link Owner} to the data store, either inserting or updating it.
|
||||||
*
|
* @param owner the {@link Owner} to save
|
||||||
* @param owner the <code>Owner</code> to save
|
|
||||||
* @see BaseEntity#isNew
|
|
||||||
*/
|
*/
|
||||||
void save(Owner owner) throws DataAccessException;
|
void save(Owner owner);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,8 @@ package org.springframework.samples.petclinic.repository;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.dao.DataAccessException;
|
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.BaseEntity;
|
||||||
import org.springframework.samples.petclinic.model.Pet;
|
import org.springframework.samples.petclinic.model.Pet;
|
||||||
import org.springframework.samples.petclinic.model.PetType;
|
import org.springframework.samples.petclinic.model.PetType;
|
||||||
|
@ -31,31 +33,27 @@ import org.springframework.samples.petclinic.model.PetType;
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @author Michael Isvy
|
* @author Michael Isvy
|
||||||
*/
|
*/
|
||||||
public interface PetRepository {
|
public interface PetRepository extends Repository<Pet, Integer> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve all <code>PetType</code>s from the data store.
|
* Retrieve all {@link PetType}s from the data store.
|
||||||
*
|
* @return a Collection of {@link PetType}s.
|
||||||
* @return a <code>Collection</code> of <code>PetType</code>s
|
|
||||||
*/
|
*/
|
||||||
List<PetType> findPetTypes() throws DataAccessException;
|
@Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name")
|
||||||
|
List<PetType> findPetTypes();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a <code>Pet</code> from the data store by id.
|
* Retrieve a {@link Pet} from the data store by id.
|
||||||
*
|
|
||||||
* @param id the id to search for
|
* @param id the id to search for
|
||||||
* @return the <code>Pet</code> if found
|
* @return the {@link Pet} if found
|
||||||
* @throws org.springframework.dao.DataRetrievalFailureException
|
|
||||||
* if not found
|
|
||||||
*/
|
*/
|
||||||
Pet findById(int id) throws DataAccessException;
|
Pet findById(int id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save a <code>Pet</code> to the data store, either inserting or updating it.
|
* Save a {@link Pet} to the data store, either inserting or updating it.
|
||||||
*
|
* @param pet the {@link Pet} to save
|
||||||
* @param pet the <code>Pet</code> to save
|
|
||||||
* @see BaseEntity#isNew
|
|
||||||
*/
|
*/
|
||||||
void save(Pet pet) throws DataAccessException;
|
void save(Pet pet);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,7 @@ package org.springframework.samples.petclinic.repository;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
import org.springframework.dao.DataAccessException;
|
import org.springframework.dao.DataAccessException;
|
||||||
|
import org.springframework.data.repository.Repository;
|
||||||
import org.springframework.samples.petclinic.model.Vet;
|
import org.springframework.samples.petclinic.model.Vet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -29,7 +30,7 @@ import org.springframework.samples.petclinic.model.Vet;
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @author Michael Isvy
|
* @author Michael Isvy
|
||||||
*/
|
*/
|
||||||
public interface VetRepository {
|
public interface VetRepository extends Repository<Vet, Integer> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve all <code>Vet</code>s from the data store.
|
* Retrieve all <code>Vet</code>s from the data store.
|
||||||
|
|
|
@ -18,6 +18,7 @@ package org.springframework.samples.petclinic.repository;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.dao.DataAccessException;
|
import org.springframework.dao.DataAccessException;
|
||||||
|
import org.springframework.data.repository.Repository;
|
||||||
import org.springframework.samples.petclinic.model.BaseEntity;
|
import org.springframework.samples.petclinic.model.BaseEntity;
|
||||||
import org.springframework.samples.petclinic.model.Visit;
|
import org.springframework.samples.petclinic.model.Visit;
|
||||||
|
|
||||||
|
@ -30,7 +31,7 @@ import org.springframework.samples.petclinic.model.Visit;
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @author Michael Isvy
|
* @author Michael Isvy
|
||||||
*/
|
*/
|
||||||
public interface VisitRepository {
|
public interface VisitRepository extends Repository<Visit, Integer> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save a <code>Visit</code> to the data store, either inserting or updating it.
|
* Save a <code>Visit</code> to the data store, either inserting or updating it.
|
||||||
|
|
|
@ -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 <i>starts</i> 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<Owner> findByLastName(String lastName) throws DataAccessException {
|
|
||||||
Map<String, Object> params = new HashMap<String, Object>();
|
|
||||||
params.put("lastName", lastName + "%");
|
|
||||||
List<Owner> 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 <code>id</code>; 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<String, Object> params = new HashMap<String, Object>();
|
|
||||||
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<String, Object> params = new HashMap<String, Object>();
|
|
||||||
params.put("id", owner.getId().intValue());
|
|
||||||
final List<JdbcPet> 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<Visit> 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<PetType> getPetTypes() throws DataAccessException {
|
|
||||||
return this.namedParameterJdbcTemplate.query(
|
|
||||||
"SELECT id, name FROM types ORDER BY name", new HashMap<String, Object>(),
|
|
||||||
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<Owner> owners) {
|
|
||||||
for (Owner owner : owners) {
|
|
||||||
loadPetsAndVisits(owner);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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<PetType> findPetTypes() throws DataAccessException {
|
|
||||||
Map<String, Object> params = new HashMap<String, Object>();
|
|
||||||
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<String, Object> params = new HashMap<String, Object>();
|
|
||||||
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<Visit> 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());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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<JdbcPet> {
|
|
||||||
|
|
||||||
@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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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<Vet> findAll() throws DataAccessException {
|
|
||||||
List<Vet> vets = new ArrayList<Vet>();
|
|
||||||
// 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<Specialty> 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<Integer> vetSpecialtiesIds = this.jdbcTemplate.query(
|
|
||||||
"SELECT specialty_id FROM vet_specialties WHERE vet_id=?",
|
|
||||||
new BeanPropertyRowMapper<Integer>() {
|
|
||||||
@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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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<Visit> findByPetId(Integer petId) {
|
|
||||||
final List<Visit> visits = this.jdbcTemplate.query(
|
|
||||||
"SELECT id, visit_date, description FROM visits WHERE pet_id=?",
|
|
||||||
new BeanPropertyRowMapper<Visit>() {
|
|
||||||
@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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
|
|
|
@ -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<Owner> 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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<PetType> 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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<Vet> findAll() {
|
|
||||||
return this.em.createQuery("SELECT distinct vet FROM Vet vet left join fetch vet.specialties ORDER BY vet.lastName, vet.firstName").getResultList();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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.
|
|
||||||
* <p/>
|
|
||||||
* <p>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<Visit> 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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
|
|
|
@ -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<Owner, Integer> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@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);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@Query("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id")
|
|
||||||
public Owner findById(@Param("id") int id);
|
|
||||||
}
|
|
|
@ -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<Pet, Integer> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name")
|
|
||||||
List<PetType> findPetTypes() throws DataAccessException;
|
|
||||||
}
|
|
|
@ -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<Vet, Integer> {
|
|
||||||
}
|
|
|
@ -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<Visit, Integer> {
|
|
||||||
}
|
|
Loading…
Reference in a new issue