diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaOwnerRepositoryExtImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaOwnerRepositoryExtImpl.java new file mode 100644 index 000000000..ff5eb437f --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaOwnerRepositoryExtImpl.java @@ -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 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); + } + + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryExtImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryExtImpl.java new file mode 100644 index 000000000..612deff10 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryExtImpl.java @@ -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 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); + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaSpecialtyRepositoryExtImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaSpecialtyRepositoryExtImpl.java new file mode 100644 index 000000000..a60bcd8d1 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaSpecialtyRepositoryExtImpl.java @@ -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 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); + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVetRepositoryExtImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVetRepositoryExtImpl.java new file mode 100644 index 000000000..057a7a025 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVetRepositoryExtImpl.java @@ -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 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); + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryExtImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryExtImpl.java new file mode 100644 index 000000000..418c0f2ab --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryExtImpl.java @@ -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 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); + } + +}