mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:15:50 +00:00
Add extended repositories implementations
This commit is contained in:
parent
8c5830c145
commit
d230db22b9
9 changed files with 205 additions and 12 deletions
|
@ -7,7 +7,7 @@ import org.springframework.samples.petclinic.model.PetType;
|
|||
|
||||
public interface PetTypeRepositoryExt {
|
||||
|
||||
PetType findById(int id);
|
||||
PetType findById(int id) throws DataAccessException;
|
||||
|
||||
Collection<PetType> findAll() throws DataAccessException;
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ import org.springframework.samples.petclinic.model.Specialty;
|
|||
|
||||
public interface SpecialtyRepositoryExt {
|
||||
|
||||
Specialty findById(int id);
|
||||
Specialty findById(int id) throws DataAccessException;
|
||||
|
||||
Collection<Specialty> findAll() throws DataAccessException;
|
||||
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
package org.springframework.samples.petclinic.repository.jdbc;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.samples.petclinic.model.PetType;
|
||||
import org.springframework.samples.petclinic.repository.PetTypeRepositoryExt;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@Qualifier("PetTypeRepositoryExt")
|
||||
public class JdbcPetTypeRepositoryExtImpl implements PetTypeRepositoryExt {
|
||||
|
||||
@Override
|
||||
public PetType findById(int id) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<PetType> findAll() throws DataAccessException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(PetType petType) throws DataAccessException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(PetType petType) throws DataAccessException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package org.springframework.samples.petclinic.repository.jdbc;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.samples.petclinic.model.Specialty;
|
||||
import org.springframework.samples.petclinic.repository.SpecialtyRepositoryExt;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@Qualifier("SpecialtyRepositoryExt")
|
||||
public class JdbcSpecialtyRepositoryExtImpl implements SpecialtyRepositoryExt {
|
||||
|
||||
@Override
|
||||
public Specialty findById(int id) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Specialty> findAll() throws DataAccessException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Specialty specialty) throws DataAccessException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Specialty specialty) throws DataAccessException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package org.springframework.samples.petclinic.repository.jpa;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.samples.petclinic.model.PetType;
|
||||
import org.springframework.samples.petclinic.repository.PetTypeRepositoryExt;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@Qualifier("PetTypeRepositoryExt")
|
||||
public class JpaPetTypeRepositoryExtImpl implements PetTypeRepositoryExt {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Override
|
||||
public PetType findById(int id) {
|
||||
return this.em.find(PetType.class, id);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Collection<PetType> findAll() throws DataAccessException {
|
||||
return this.em.createQuery("SELECT ptype FROM PetType ptype").getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(PetType petType) throws DataAccessException {
|
||||
if (petType.getId() == null) {
|
||||
this.em.persist(petType);
|
||||
} else {
|
||||
this.em.merge(petType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(PetType petType) throws DataAccessException {
|
||||
this.em.remove(petType);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -5,12 +5,14 @@ import java.util.Collection;
|
|||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.samples.petclinic.model.Specialty;
|
||||
import org.springframework.samples.petclinic.repository.SpecialtyRepositoryExt;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@Qualifier("SpecialtyRepositoryExt")
|
||||
public class JpaSpecialtyRepositoryExtImpl implements SpecialtyRepositoryExt {
|
||||
|
||||
@PersistenceContext
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package org.springframework.samples.petclinic.repository.springdatajpa;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.samples.petclinic.model.PetType;
|
||||
import org.springframework.samples.petclinic.repository.PetTypeRepositoryExt;
|
||||
|
||||
@Qualifier("PetTypeRepositoryExt")
|
||||
public interface SpringDataPetTypeRepositoryExt extends PetTypeRepositoryExt, Repository<PetType, Integer> {
|
||||
|
||||
}
|
|
@ -4,6 +4,7 @@ import java.util.Collection;
|
|||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.samples.petclinic.model.Owner;
|
||||
import org.springframework.samples.petclinic.model.Pet;
|
||||
|
@ -21,7 +22,7 @@ import org.springframework.stereotype.Service;
|
|||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class ClinicServiceExtImpl extends ClinicServiceImpl implements ClinicServiceExt {
|
||||
public class ClinicServiceExtImpl implements ClinicServiceExt {
|
||||
|
||||
private SpecialtyRepositoryExt specialtyRepositoryExt;
|
||||
private PetTypeRepositoryExt petTypeRepositoryExt;
|
||||
|
@ -38,7 +39,6 @@ public class ClinicServiceExtImpl extends ClinicServiceImpl implements ClinicSer
|
|||
@Qualifier("VisitRepositoryExt") VisitRepositoryExt visitRepositoryExt,
|
||||
@Qualifier("SpecialtyRepositoryExt") SpecialtyRepositoryExt specialtyRepositoryExt,
|
||||
@Qualifier("PetTypeRepositoryExt") PetTypeRepositoryExt petTypeRepositoryExt) {
|
||||
super(petRepositoryExt, vetRepositoryExt, ownerRepositoryExt, visitRepositoryExt);
|
||||
this.specialtyRepositoryExt = specialtyRepositoryExt;
|
||||
this.petTypeRepositoryExt = petTypeRepositoryExt;
|
||||
this.petRepositoryExt = petRepositoryExt;
|
||||
|
@ -161,4 +161,62 @@ public class ClinicServiceExtImpl extends ClinicServiceImpl implements ClinicSer
|
|||
specialtyRepositoryExt.delete(specialty);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Collection<PetType> findPetTypes() throws DataAccessException {
|
||||
return petRepositoryExt.findPetTypes();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Owner findOwnerById(int id) throws DataAccessException {
|
||||
return ownerRepositoryExt.findById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Pet findPetById(int id) throws DataAccessException {
|
||||
return petRepositoryExt.findById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void savePet(Pet pet) throws DataAccessException {
|
||||
petRepositoryExt.save(pet);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void saveVisit(Visit visit) throws DataAccessException {
|
||||
visitRepositoryExt.save(visit);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
@Cacheable(value = "vets")
|
||||
public Collection<Vet> findVets() throws DataAccessException {
|
||||
return vetRepositoryExt.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void saveOwner(Owner owner) throws DataAccessException {
|
||||
ownerRepositoryExt.save(owner);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Collection<Owner> findOwnerByLastName(String lastName) throws DataAccessException {
|
||||
return ownerRepositoryExt.findByLastName(lastName);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Collection<Visit> findVisitsByPetId(int petId) {
|
||||
return visitRepositoryExt.findByPetId(petId);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -49,14 +49,10 @@ public class ClinicServiceImpl implements ClinicService {
|
|||
|
||||
@Autowired
|
||||
public ClinicServiceImpl(
|
||||
// @Qualifier("PetRepository") PetRepository petRepository,
|
||||
// @Qualifier("VetRepository") VetRepository vetRepository,
|
||||
// @Qualifier("OwnerRepository") OwnerRepository ownerRepository,
|
||||
// @Qualifier("VisitRepository") VisitRepository visitRepository) {
|
||||
@Qualifier("PetRepositoryExt") PetRepository petRepository,
|
||||
@Qualifier("VetRepositoryExt") VetRepository vetRepository,
|
||||
@Qualifier("OwnerRepositoryExt") OwnerRepository ownerRepository,
|
||||
@Qualifier("VisitRepositoryExt") VisitRepository visitRepository) {
|
||||
@Qualifier("PetRepository") PetRepository petRepository,
|
||||
@Qualifier("VetRepository") VetRepository vetRepository,
|
||||
@Qualifier("OwnerRepository") OwnerRepository ownerRepository,
|
||||
@Qualifier("VisitRepository") VisitRepository visitRepository) {
|
||||
this.petRepository = petRepository;
|
||||
this.vetRepository = vetRepository;
|
||||
this.ownerRepository = ownerRepository;
|
||||
|
|
Loading…
Reference in a new issue