mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:15:50 +00:00
fix delete() method in JpaPetRepositoryExtImpl
This commit is contained in:
parent
b6dc54f743
commit
720448b506
1 changed files with 44 additions and 39 deletions
|
@ -1,39 +1,44 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
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.Pet;
|
||||
import org.springframework.samples.petclinic.repository.PetRepositoryExt;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* @author Vitaliy Fedoriv
|
||||
*
|
||||
*/
|
||||
@Repository
|
||||
@Qualifier("PetRepositoryExt")
|
||||
public class JpaPetRepositoryExtImpl extends JpaPetRepositoryImpl implements PetRepositoryExt {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Collection<Pet> findAll() throws DataAccessException {
|
||||
return this.em.createQuery("SELECT pet FROM Pet pet").getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Pet pet) throws DataAccessException {
|
||||
this.em.remove(this.em.contains(pet) ? pet : this.em.merge(pet));
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.springframework.samples.petclinic.repository.jpa;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
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.Owner;
|
||||
import org.springframework.samples.petclinic.model.Pet;
|
||||
import org.springframework.samples.petclinic.model.Visit;
|
||||
import org.springframework.samples.petclinic.repository.PetRepositoryExt;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* @author Vitaliy Fedoriv
|
||||
*
|
||||
*/
|
||||
@Repository
|
||||
@Qualifier("PetRepositoryExt")
|
||||
public class JpaPetRepositoryExtImpl extends JpaPetRepositoryImpl implements PetRepositoryExt {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Collection<Pet> findAll() throws DataAccessException {
|
||||
return this.em.createQuery("SELECT pet FROM Pet pet").getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Pet pet) throws DataAccessException {
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue