mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 13:05:49 +00:00
Fix "Removing a detached instance" issue when delete
This commit is contained in:
parent
7f5e9046fb
commit
dbb06c1099
12 changed files with 30 additions and 19 deletions
|
@ -22,18 +22,13 @@ public class JpaOwnerRepositoryExtImpl extends JpaOwnerRepositoryImpl implements
|
|||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Collection<Owner> findAll() throws DataAccessException {
|
||||
// TODO Select only owner or still join with pets ?
|
||||
Query query = this.em.createQuery("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets");
|
||||
Query query = this.em.createQuery("SELECT owner FROM Owner owner");
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Owner owner) throws DataAccessException {
|
||||
// TODO need null check, throw Exception etc ?
|
||||
if(!(owner.getId() == null)){
|
||||
this.em.remove(owner);
|
||||
}
|
||||
|
||||
this.em.remove(this.em.contains(owner) ? owner : this.em.merge(owner));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ public class JpaPetRepositoryExtImpl extends JpaPetRepositoryImpl implements Pet
|
|||
|
||||
@Override
|
||||
public void delete(Pet pet) throws DataAccessException {
|
||||
this.em.remove(pet);
|
||||
this.em.remove(this.em.contains(pet) ? pet : this.em.merge(pet));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -41,8 +41,7 @@ public class JpaPetTypeRepositoryExtImpl implements PetTypeRepositoryExt {
|
|||
|
||||
@Override
|
||||
public void delete(PetType petType) throws DataAccessException {
|
||||
this.em.remove(petType);
|
||||
|
||||
this.em.remove(this.em.contains(petType) ? petType : this.em.merge(petType));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ public class JpaSpecialtyRepositoryExtImpl implements SpecialtyRepositoryExt {
|
|||
|
||||
@Override
|
||||
public void delete(Specialty specialty) throws DataAccessException {
|
||||
this.em.remove(specialty);
|
||||
this.em.remove(this.em.contains(specialty) ? specialty : this.em.merge(specialty));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ public class JpaVetRepositoryExtImpl extends JpaVetRepositoryImpl implements Vet
|
|||
|
||||
@Override
|
||||
public void delete(Vet vet) throws DataAccessException {
|
||||
this.em.remove(vet);
|
||||
this.em.remove(this.em.contains(vet) ? vet : this.em.merge(vet));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -32,8 +32,7 @@ public class JpaVisitRepositoryExtImpl extends JpaVisitRepositoryImpl implements
|
|||
|
||||
@Override
|
||||
public void delete(Visit visit) throws DataAccessException {
|
||||
// TODO Auto-generated method stub
|
||||
this.em.remove(visit);
|
||||
this.em.remove(this.em.contains(visit) ? visit : this.em.merge(visit));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ package org.springframework.samples.petclinic.rest;
|
|||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -26,14 +27,12 @@ import org.springframework.http.HttpStatus;
|
|||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.samples.petclinic.model.Owner;
|
||||
import org.springframework.samples.petclinic.model.Pet;
|
||||
import org.springframework.samples.petclinic.service.ClinicServiceExt;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
|
@ -49,8 +48,8 @@ public class OwnerRestController {
|
|||
@Autowired
|
||||
private ClinicServiceExt clinicService;
|
||||
|
||||
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
public ResponseEntity<Collection<Owner>> getOwnersList(@RequestParam("lastName") String ownerLastName){
|
||||
@RequestMapping(value = "/*/lastname/{lastName}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
public ResponseEntity<Collection<Owner>> getOwnersList(@PathVariable("lastName") String ownerLastName){
|
||||
if (ownerLastName == null){
|
||||
ownerLastName = "";
|
||||
}
|
||||
|
@ -61,6 +60,15 @@ public class OwnerRestController {
|
|||
return new ResponseEntity<Collection<Owner>>(owners, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
public ResponseEntity<Collection<Owner>> getOwners(){
|
||||
Collection<Owner> owners = this.clinicService.findAllOwners();
|
||||
if(owners.isEmpty()){
|
||||
return new ResponseEntity<Collection<Owner>>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
return new ResponseEntity<Collection<Owner>>(owners, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/{ownerId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
public ResponseEntity<Owner> getOwner(@PathVariable("ownerId") int ownerId){
|
||||
Owner owner = this.clinicService.findOwnerById(ownerId);
|
||||
|
@ -100,6 +108,7 @@ public class OwnerRestController {
|
|||
}
|
||||
|
||||
@RequestMapping(value = "/{ownerId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@Transactional
|
||||
public ResponseEntity<Void> deleteOwner(@PathVariable("ownerId") int ownerId){
|
||||
Owner owner = this.clinicService.findOwnerById(ownerId);
|
||||
if(owner == null){
|
||||
|
|
|
@ -18,6 +18,7 @@ package org.springframework.samples.petclinic.rest;
|
|||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -95,13 +96,13 @@ public class PetRestController {
|
|||
}
|
||||
|
||||
@RequestMapping(value = "/{petId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@Transactional
|
||||
public ResponseEntity<Void> deletePet(@PathVariable("petId") int petId){
|
||||
Pet pet = this.clinicService.findPetById(petId);
|
||||
if(pet == null){
|
||||
return new ResponseEntity<Void>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
this.clinicService.deletePet(pet);
|
||||
// TODO delete error - FK etc.
|
||||
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.springframework.samples.petclinic.rest;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -73,6 +74,7 @@ public class PetTypeRestController {
|
|||
}
|
||||
|
||||
@RequestMapping(value = "/{petTypeId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@Transactional
|
||||
public ResponseEntity<Void> deletePetType(@PathVariable("petTypeId") int petTypeId){
|
||||
PetType petType = this.clinicService.findPetTypeById(petTypeId);
|
||||
if(petType == null){
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.springframework.samples.petclinic.rest;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -73,6 +74,7 @@ public class SpecialtyRestController {
|
|||
}
|
||||
|
||||
@RequestMapping(value = "/{specialtyId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@Transactional
|
||||
public ResponseEntity<Void> deleteSpecialty(@PathVariable("specialtyId") int specialtyId){
|
||||
Specialty specialty = this.clinicService.findSpecialtyById(specialtyId);
|
||||
if(specialty == null){
|
||||
|
|
|
@ -18,6 +18,7 @@ package org.springframework.samples.petclinic.rest;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -94,6 +95,7 @@ public class VetRestController {
|
|||
}
|
||||
|
||||
@RequestMapping(value = "/{vetId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@Transactional
|
||||
public ResponseEntity<Void> deleteVet(@PathVariable("vetId") int vetId){
|
||||
Vet vet = this.clinicService.findVetById(vetId);
|
||||
if(vet == null){
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.springframework.samples.petclinic.rest;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -75,6 +76,7 @@ public class VisitRestController {
|
|||
}
|
||||
|
||||
@RequestMapping(value = "/{visitId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@Transactional
|
||||
public ResponseEntity<Void> deleteVisit(@PathVariable("visitId") int visitId){
|
||||
Visit visit = this.clinicService.findVisitById(visitId);
|
||||
if(visit == null){
|
||||
|
|
Loading…
Reference in a new issue