mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:15:50 +00:00
add some exception handling in ClinicServiceExtImpl
This commit is contained in:
parent
50da6d65fb
commit
bbae5a66f9
1 changed files with 71 additions and 6 deletions
|
@ -1,3 +1,19 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2016 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.service;
|
package org.springframework.samples.petclinic.service;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -6,6 +22,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
import org.springframework.dao.DataAccessException;
|
import org.springframework.dao.DataAccessException;
|
||||||
|
import org.springframework.dao.EmptyResultDataAccessException;
|
||||||
|
import org.springframework.orm.ObjectRetrievalFailureException;
|
||||||
import org.springframework.samples.petclinic.model.Owner;
|
import org.springframework.samples.petclinic.model.Owner;
|
||||||
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;
|
||||||
|
@ -21,6 +39,11 @@ import org.springframework.samples.petclinic.repository.VisitRepositoryExt;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Vitaliy Fedoriv
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Qualifier("ClinicServiceExt")
|
@Qualifier("ClinicServiceExt")
|
||||||
public class ClinicServiceExtImpl implements ClinicServiceExt {
|
public class ClinicServiceExtImpl implements ClinicServiceExt {
|
||||||
|
@ -63,7 +86,14 @@ public class ClinicServiceExtImpl implements ClinicServiceExt {
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Visit findVisitById(int visitId) throws DataAccessException {
|
public Visit findVisitById(int visitId) throws DataAccessException {
|
||||||
return visitRepositoryExt.findById(visitId);
|
Visit visit = null;
|
||||||
|
try {
|
||||||
|
visit = visitRepositoryExt.findById(visitId);
|
||||||
|
} catch (ObjectRetrievalFailureException|EmptyResultDataAccessException e) {
|
||||||
|
// just ignore not found exceptions for Jdbc/Jpa realization
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return visit;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -81,7 +111,14 @@ public class ClinicServiceExtImpl implements ClinicServiceExt {
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Vet findVetById(int id) throws DataAccessException {
|
public Vet findVetById(int id) throws DataAccessException {
|
||||||
return vetRepositoryExt.findById(id);
|
Vet vet = null;
|
||||||
|
try {
|
||||||
|
vet = vetRepositoryExt.findById(id);
|
||||||
|
} catch (ObjectRetrievalFailureException|EmptyResultDataAccessException e) {
|
||||||
|
// just ignore not found exceptions for Jdbc/Jpa realization
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return vet;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -117,7 +154,14 @@ public class ClinicServiceExtImpl implements ClinicServiceExt {
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public PetType findPetTypeById(int petTypeId) {
|
public PetType findPetTypeById(int petTypeId) {
|
||||||
return petTypeRepositoryExt.findById(petTypeId);
|
PetType petType = null;
|
||||||
|
try {
|
||||||
|
petType = petTypeRepositoryExt.findById(petTypeId);
|
||||||
|
} catch (ObjectRetrievalFailureException|EmptyResultDataAccessException e) {
|
||||||
|
// just ignore not found exceptions for Jdbc/Jpa realization
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return petType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -141,7 +185,14 @@ public class ClinicServiceExtImpl implements ClinicServiceExt {
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Specialty findSpecialtyById(int specialtyId) {
|
public Specialty findSpecialtyById(int specialtyId) {
|
||||||
return specialtyRepositoryExt.findById(specialtyId);
|
Specialty specialty = null;
|
||||||
|
try {
|
||||||
|
specialty = specialtyRepositoryExt.findById(specialtyId);
|
||||||
|
} catch (ObjectRetrievalFailureException|EmptyResultDataAccessException e) {
|
||||||
|
// just ignore not found exceptions for Jdbc/Jpa realization
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return specialty;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -171,13 +222,27 @@ public class ClinicServiceExtImpl implements ClinicServiceExt {
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Owner findOwnerById(int id) throws DataAccessException {
|
public Owner findOwnerById(int id) throws DataAccessException {
|
||||||
return ownerRepositoryExt.findById(id);
|
Owner owner = null;
|
||||||
|
try {
|
||||||
|
owner = ownerRepositoryExt.findById(id);
|
||||||
|
} catch (ObjectRetrievalFailureException|EmptyResultDataAccessException e) {
|
||||||
|
// just ignore not found exceptions for Jdbc/Jpa realization
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Pet findPetById(int id) throws DataAccessException {
|
public Pet findPetById(int id) throws DataAccessException {
|
||||||
return petRepositoryExt.findById(id);
|
Pet pet = null;
|
||||||
|
try {
|
||||||
|
pet = petRepositoryExt.findById(id);
|
||||||
|
} catch (ObjectRetrievalFailureException|EmptyResultDataAccessException e) {
|
||||||
|
// just ignore not found exceptions for Jdbc/Jpa realization
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return pet;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue