mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 13:05:49 +00:00
Add JPA implementation of extended repositories
This commit is contained in:
parent
8be14d6b75
commit
a9563d60f0
5 changed files with 207 additions and 0 deletions
|
@ -0,0 +1,39 @@
|
|||
package org.springframework.samples.petclinic.repository.jpa;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.samples.petclinic.model.Owner;
|
||||
import org.springframework.samples.petclinic.repository.OwnerRepositoryExt;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@Qualifier("OwnerRepositoryExt")
|
||||
public class JpaOwnerRepositoryExtImpl extends JpaOwnerRepositoryImpl implements OwnerRepositoryExt {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@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");
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
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(pet);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package org.springframework.samples.petclinic.repository.jpa;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.samples.petclinic.model.Specialty;
|
||||
import org.springframework.samples.petclinic.repository.SpecialtyRepositoryExt;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class JpaSpecialtyRepositoryExtImpl implements SpecialtyRepositoryExt {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Override
|
||||
public Specialty findById(int id) {
|
||||
return this.em.find(Specialty.class, id);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Collection<Specialty> findAll() throws DataAccessException {
|
||||
return this.em.createQuery("SELECT s FROM Specialty s").getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Specialty specialty) throws DataAccessException {
|
||||
if (specialty.getId() == null) {
|
||||
this.em.persist(specialty);
|
||||
} else {
|
||||
this.em.merge(specialty);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Specialty specialty) throws DataAccessException {
|
||||
this.em.remove(specialty);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
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.Vet;
|
||||
import org.springframework.samples.petclinic.repository.VetRepositoryExt;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@Qualifier("VetRepositoryExt")
|
||||
public class JpaVetRepositoryExtImpl extends JpaVetRepositoryImpl implements VetRepositoryExt {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Override
|
||||
public Vet findById(int id) throws DataAccessException {
|
||||
return this.em.find(Vet.class, id);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Collection<Vet> findAll() throws DataAccessException {
|
||||
return this.em.createQuery("SELECT vet FROM Vet vet").getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Vet vet) throws DataAccessException {
|
||||
if (vet.getId() == null) {
|
||||
this.em.persist(vet);
|
||||
} else {
|
||||
this.em.merge(vet);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Vet vet) throws DataAccessException {
|
||||
this.em.remove(vet);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package org.springframework.samples.petclinic.repository.jpa;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.samples.petclinic.model.Visit;
|
||||
import org.springframework.samples.petclinic.repository.VisitRepositoryExt;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@Qualifier("VisitRepositoryExt")
|
||||
public class JpaVisitRepositoryExtImpl extends JpaVisitRepositoryImpl implements VisitRepositoryExt {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Override
|
||||
public Visit findById(int id) throws DataAccessException {
|
||||
return this.em.find(Visit.class, id);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Collection<Visit> findAll() throws DataAccessException {
|
||||
return this.em.createQuery("SELECT v FROM Visit v").getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Visit visit) throws DataAccessException {
|
||||
// TODO Auto-generated method stub
|
||||
this.em.remove(visit);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue