Fix "Removing a detached instance" issue when delete

This commit is contained in:
Vitaliy Fedoriv 2016-11-06 11:19:11 +02:00
parent 7f5e9046fb
commit dbb06c1099
12 changed files with 30 additions and 19 deletions

View file

@ -22,18 +22,13 @@ public class JpaOwnerRepositoryExtImpl extends JpaOwnerRepositoryImpl implements
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public Collection<Owner> findAll() throws DataAccessException { public Collection<Owner> findAll() throws DataAccessException {
// TODO Select only owner or still join with pets ? Query query = this.em.createQuery("SELECT owner FROM Owner owner");
Query query = this.em.createQuery("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets");
return query.getResultList(); return query.getResultList();
} }
@Override @Override
public void delete(Owner owner) throws DataAccessException { public void delete(Owner owner) throws DataAccessException {
// TODO need null check, throw Exception etc ? this.em.remove(this.em.contains(owner) ? owner : this.em.merge(owner));
if(!(owner.getId() == null)){
this.em.remove(owner);
}
} }
} }

View file

@ -33,7 +33,7 @@ public class JpaPetRepositoryExtImpl extends JpaPetRepositoryImpl implements Pet
@Override @Override
public void delete(Pet pet) throws DataAccessException { public void delete(Pet pet) throws DataAccessException {
this.em.remove(pet); this.em.remove(this.em.contains(pet) ? pet : this.em.merge(pet));
} }
} }

View file

@ -41,8 +41,7 @@ public class JpaPetTypeRepositoryExtImpl implements PetTypeRepositoryExt {
@Override @Override
public void delete(PetType petType) throws DataAccessException { public void delete(PetType petType) throws DataAccessException {
this.em.remove(petType); this.em.remove(this.em.contains(petType) ? petType : this.em.merge(petType));
} }
} }

View file

@ -40,7 +40,7 @@ public class JpaSpecialtyRepositoryExtImpl implements SpecialtyRepositoryExt {
@Override @Override
public void delete(Specialty specialty) throws DataAccessException { public void delete(Specialty specialty) throws DataAccessException {
this.em.remove(specialty); this.em.remove(this.em.contains(specialty) ? specialty : this.em.merge(specialty));
} }
} }

View file

@ -40,7 +40,7 @@ public class JpaVetRepositoryExtImpl extends JpaVetRepositoryImpl implements Vet
@Override @Override
public void delete(Vet vet) throws DataAccessException { public void delete(Vet vet) throws DataAccessException {
this.em.remove(vet); this.em.remove(this.em.contains(vet) ? vet : this.em.merge(vet));
} }
} }

View file

@ -32,8 +32,7 @@ public class JpaVisitRepositoryExtImpl extends JpaVisitRepositoryImpl implements
@Override @Override
public void delete(Visit visit) throws DataAccessException { public void delete(Visit visit) throws DataAccessException {
// TODO Auto-generated method stub this.em.remove(this.em.contains(visit) ? visit : this.em.merge(visit));
this.em.remove(visit);
} }
} }

View file

@ -18,6 +18,7 @@ package org.springframework.samples.petclinic.rest;
import java.util.Collection; import java.util.Collection;
import javax.transaction.Transactional;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; 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.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
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.service.ClinicServiceExt; import org.springframework.samples.petclinic.service.ClinicServiceExt;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; 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.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
@ -49,8 +48,8 @@ public class OwnerRestController {
@Autowired @Autowired
private ClinicServiceExt clinicService; private ClinicServiceExt clinicService;
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @RequestMapping(value = "/*/lastname/{lastName}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Collection<Owner>> getOwnersList(@RequestParam("lastName") String ownerLastName){ public ResponseEntity<Collection<Owner>> getOwnersList(@PathVariable("lastName") String ownerLastName){
if (ownerLastName == null){ if (ownerLastName == null){
ownerLastName = ""; ownerLastName = "";
} }
@ -61,6 +60,15 @@ public class OwnerRestController {
return new ResponseEntity<Collection<Owner>>(owners, HttpStatus.OK); 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) @RequestMapping(value = "/{ownerId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Owner> getOwner(@PathVariable("ownerId") int ownerId){ public ResponseEntity<Owner> getOwner(@PathVariable("ownerId") int ownerId){
Owner owner = this.clinicService.findOwnerById(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) @RequestMapping(value = "/{ownerId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Transactional
public ResponseEntity<Void> deleteOwner(@PathVariable("ownerId") int ownerId){ public ResponseEntity<Void> deleteOwner(@PathVariable("ownerId") int ownerId){
Owner owner = this.clinicService.findOwnerById(ownerId); Owner owner = this.clinicService.findOwnerById(ownerId);
if(owner == null){ if(owner == null){

View file

@ -18,6 +18,7 @@ package org.springframework.samples.petclinic.rest;
import java.util.Collection; import java.util.Collection;
import javax.transaction.Transactional;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; 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) @RequestMapping(value = "/{petId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Transactional
public ResponseEntity<Void> deletePet(@PathVariable("petId") int petId){ public ResponseEntity<Void> deletePet(@PathVariable("petId") int petId){
Pet pet = this.clinicService.findPetById(petId); Pet pet = this.clinicService.findPetById(petId);
if(pet == null){ if(pet == null){
return new ResponseEntity<Void>(HttpStatus.NOT_FOUND); return new ResponseEntity<Void>(HttpStatus.NOT_FOUND);
} }
this.clinicService.deletePet(pet); this.clinicService.deletePet(pet);
// TODO delete error - FK etc.
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT); return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
} }

View file

@ -3,6 +3,7 @@ package org.springframework.samples.petclinic.rest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import javax.transaction.Transactional;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; 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) @RequestMapping(value = "/{petTypeId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Transactional
public ResponseEntity<Void> deletePetType(@PathVariable("petTypeId") int petTypeId){ public ResponseEntity<Void> deletePetType(@PathVariable("petTypeId") int petTypeId){
PetType petType = this.clinicService.findPetTypeById(petTypeId); PetType petType = this.clinicService.findPetTypeById(petTypeId);
if(petType == null){ if(petType == null){

View file

@ -3,6 +3,7 @@ package org.springframework.samples.petclinic.rest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import javax.transaction.Transactional;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; 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) @RequestMapping(value = "/{specialtyId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Transactional
public ResponseEntity<Void> deleteSpecialty(@PathVariable("specialtyId") int specialtyId){ public ResponseEntity<Void> deleteSpecialty(@PathVariable("specialtyId") int specialtyId){
Specialty specialty = this.clinicService.findSpecialtyById(specialtyId); Specialty specialty = this.clinicService.findSpecialtyById(specialtyId);
if(specialty == null){ if(specialty == null){

View file

@ -18,6 +18,7 @@ package org.springframework.samples.petclinic.rest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import javax.transaction.Transactional;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; 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) @RequestMapping(value = "/{vetId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Transactional
public ResponseEntity<Void> deleteVet(@PathVariable("vetId") int vetId){ public ResponseEntity<Void> deleteVet(@PathVariable("vetId") int vetId){
Vet vet = this.clinicService.findVetById(vetId); Vet vet = this.clinicService.findVetById(vetId);
if(vet == null){ if(vet == null){

View file

@ -3,6 +3,7 @@ package org.springframework.samples.petclinic.rest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import javax.transaction.Transactional;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; 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) @RequestMapping(value = "/{visitId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Transactional
public ResponseEntity<Void> deleteVisit(@PathVariable("visitId") int visitId){ public ResponseEntity<Void> deleteVisit(@PathVariable("visitId") int visitId){
Visit visit = this.clinicService.findVisitById(visitId); Visit visit = this.clinicService.findVisitById(visitId);
if(visit == null){ if(visit == null){