mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:15:50 +00:00
override delete() method in SpringData Pet and Visit repositories
This commit is contained in:
parent
af91fbd24d
commit
e4499ee645
6 changed files with 61 additions and 2 deletions
|
@ -0,0 +1,9 @@
|
||||||
|
package org.springframework.samples.petclinic.repository.springdatajpa;
|
||||||
|
|
||||||
|
import org.springframework.samples.petclinic.model.Pet;
|
||||||
|
|
||||||
|
public interface PetRepositoryExtOverride {
|
||||||
|
|
||||||
|
public void delete(Pet pet);
|
||||||
|
|
||||||
|
}
|
|
@ -11,7 +11,7 @@ import org.springframework.samples.petclinic.model.PetType;
|
||||||
import org.springframework.samples.petclinic.repository.PetRepositoryExt;
|
import org.springframework.samples.petclinic.repository.PetRepositoryExt;
|
||||||
|
|
||||||
@Qualifier("PetRepositoryExt")
|
@Qualifier("PetRepositoryExt")
|
||||||
public interface SpringDataPetRepositoryExt extends PetRepositoryExt, Repository<Pet, Integer> {
|
public interface SpringDataPetRepositoryExt extends PetRepositoryExt, Repository<Pet, Integer>, PetRepositoryExtOverride {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name")
|
@Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name")
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
package org.springframework.samples.petclinic.repository.springdatajpa;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
|
||||||
|
import org.springframework.samples.petclinic.model.Pet;
|
||||||
|
|
||||||
|
public class SpringDataPetRepositoryExtImpl implements PetRepositoryExtOverride {
|
||||||
|
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(Pet pet) {
|
||||||
|
String petId = pet.getId().toString();
|
||||||
|
this.em.createQuery("DELETE FROM Visit visit WHERE pet_id=" + petId).executeUpdate();
|
||||||
|
this.em.createQuery("DELETE FROM Pet pet WHERE id=" + petId).executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -6,6 +6,6 @@ import org.springframework.samples.petclinic.model.Visit;
|
||||||
import org.springframework.samples.petclinic.repository.VisitRepositoryExt;
|
import org.springframework.samples.petclinic.repository.VisitRepositoryExt;
|
||||||
|
|
||||||
@Qualifier("VisitRepositoryExt")
|
@Qualifier("VisitRepositoryExt")
|
||||||
public interface SpringDataVisitRepositoryExt extends VisitRepositoryExt, Repository<Visit, Integer> {
|
public interface SpringDataVisitRepositoryExt extends VisitRepositoryExt, Repository<Visit, Integer>, VisitRepositoryExtOverride {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
package org.springframework.samples.petclinic.repository.springdatajpa;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
|
||||||
|
import org.springframework.dao.DataAccessException;
|
||||||
|
import org.springframework.samples.petclinic.model.Visit;
|
||||||
|
|
||||||
|
public class SpringDataVisitRepositoryExtImpl implements VisitRepositoryExtOverride {
|
||||||
|
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(Visit visit) throws DataAccessException {
|
||||||
|
String visitId = visit.getId().toString();
|
||||||
|
this.em.createQuery("DELETE FROM Visit visit WHERE id=" + visitId).executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package org.springframework.samples.petclinic.repository.springdatajpa;
|
||||||
|
|
||||||
|
import org.springframework.samples.petclinic.model.Visit;
|
||||||
|
|
||||||
|
public interface VisitRepositoryExtOverride {
|
||||||
|
|
||||||
|
public void delete(Visit visit);
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue