View Javadoc

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