EntityManagerClinic.java
上传用户:dezhong
上传日期:2022-08-10
资源大小:167k
文件大小:3k
源码类别:

Java编程

开发平台:

Java

  1. package org.springframework.samples.petclinic.jpa;
  2. import java.util.Collection;
  3. import javax.persistence.EntityManager;
  4. import javax.persistence.PersistenceContext;
  5. import javax.persistence.Query;
  6. import org.springframework.samples.petclinic.Clinic;
  7. import org.springframework.samples.petclinic.Owner;
  8. import org.springframework.samples.petclinic.Pet;
  9. import org.springframework.samples.petclinic.PetType;
  10. import org.springframework.samples.petclinic.Vet;
  11. import org.springframework.samples.petclinic.Visit;
  12. import org.springframework.stereotype.Repository;
  13. import org.springframework.transaction.annotation.Transactional;
  14. /**
  15.  * JPA implementation of the Clinic interface using EntityManager.
  16.  *
  17.  * <p>The mappings are defined in "orm.xml" located in the META-INF directory.
  18.  *
  19.  * @author Mike Keith
  20.  * @author Rod Johnson
  21.  * @author Sam Brannen
  22.  * @since 22.4.2006
  23.  */
  24. @Repository
  25. @Transactional
  26. public class EntityManagerClinic implements Clinic {
  27. @PersistenceContext
  28. private EntityManager em;
  29. @Transactional(readOnly = true)
  30. @SuppressWarnings("unchecked")
  31. public Collection<Vet> getVets() {
  32. return this.em.createQuery("SELECT vet FROM Vet vet ORDER BY vet.lastName, vet.firstName").getResultList();
  33. }
  34. @Transactional(readOnly = true)
  35. @SuppressWarnings("unchecked")
  36. public Collection<PetType> getPetTypes() {
  37. return this.em.createQuery("SELECT ptype FROM PetType ptype ORDER BY ptype.name").getResultList();
  38. }
  39. @Transactional(readOnly = true)
  40. @SuppressWarnings("unchecked")
  41. public Collection<Owner> findOwners(String lastName) {
  42. Query query = this.em.createQuery("SELECT owner FROM Owner owner WHERE owner.lastName LIKE :lastName");
  43. query.setParameter("lastName", lastName + "%");
  44. return query.getResultList();
  45. }
  46. @Transactional(readOnly = true)
  47. public Owner loadOwner(int id) {
  48. return this.em.find(Owner.class, id);
  49. }
  50. @Transactional(readOnly = true)
  51. public Pet loadPet(int id) {
  52. return this.em.find(Pet.class, id);
  53. }
  54. public void storeOwner(Owner owner) {
  55. // Consider returning the persistent object here, for exposing
  56. // a newly assigned id using any persistence provider...
  57. Owner merged = this.em.merge(owner);
  58. this.em.flush();
  59. owner.setId(merged.getId());
  60. }
  61. public void storePet(Pet pet) {
  62. // Consider returning the persistent object here, for exposing
  63. // a newly assigned id using any persistence provider...
  64. Pet merged = this.em.merge(pet);
  65. this.em.flush();
  66. pet.setId(merged.getId());
  67. }
  68. public void storeVisit(Visit visit) {
  69. // Consider returning the persistent object here, for exposing
  70. // a newly assigned id using any persistence provider...
  71. Visit merged = this.em.merge(visit);
  72. this.em.flush();
  73. visit.setId(merged.getId());
  74. }
  75. }