View Javadoc

1   package org.springframework.samples.petclinic.hibernate;
2   
3   import java.util.Collection;
4   
5   import org.hibernate.SessionFactory;
6   
7   import org.springframework.beans.factory.annotation.Autowired;
8   import org.springframework.dao.DataAccessException;
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  
18  /**
19   * Hibernate implementation of the Clinic interface.
20   *
21   * <p>The mappings are defined in "petclinic.hbm.xml", located in the root of the
22   * class path.
23   *
24   * <p>Note that transactions are declared with annotations and that some methods
25   * contain "readOnly = true" which is an optimization that is particularly
26   * valuable when using Hibernate (to suppress unnecessary flush attempts for
27   * read-only operations).
28   *
29   * @author Juergen Hoeller
30   * @author Sam Brannen
31   * @author Mark Fisher
32   * @since 19.10.2003
33   */
34  @Repository
35  @Transactional
36  public class HibernateClinic implements Clinic {
37  
38  	private SessionFactory sessionFactory;
39  	
40  	@Autowired
41  	public HibernateClinic(SessionFactory sessionFactory) {
42  		this.sessionFactory = sessionFactory;
43  	}
44  
45  	@Transactional(readOnly = true)
46  	@SuppressWarnings("unchecked")
47  	public Collection<Vet> getVets() {
48  		return sessionFactory.getCurrentSession().createQuery("from Vet vet order by vet.lastName, vet.firstName").list();
49  	}
50  
51  	@Transactional(readOnly = true)
52  	@SuppressWarnings("unchecked")
53  	public Collection<PetType> getPetTypes() {
54  		return sessionFactory.getCurrentSession().createQuery("from PetType type order by type.name").list();
55  	}
56  
57  	@Transactional(readOnly = true)
58  	@SuppressWarnings("unchecked")
59  	public Collection<Owner> findOwners(String lastName) {
60  		return sessionFactory.getCurrentSession().createQuery("from Owner owner where owner.lastName like :lastName")
61  				.setString("lastName", lastName + "%").list();
62  	}
63  
64  	@Transactional(readOnly = true)
65  	public Owner loadOwner(int id) {
66  		return (Owner) sessionFactory.getCurrentSession().load(Owner.class, id);
67  	}
68  
69  	@Transactional(readOnly = true)
70  	public Pet loadPet(int id) {
71  		return (Pet) sessionFactory.getCurrentSession().load(Pet.class, id);
72  	}
73  
74  	public void storeOwner(Owner owner) {
75  		// Note: Hibernate3's merge operation does not reassociate the object
76  		// with the current Hibernate Session. Instead, it will always copy the
77  		// state over to a registered representation of the entity. In case of a
78  		// new entity, it will register a copy as well, but will not update the
79  		// id of the passed-in object. To still update the ids of the original
80  		// objects too, we need to register Spring's
81  		// IdTransferringMergeEventListener on our SessionFactory.
82  		sessionFactory.getCurrentSession().merge(owner);
83  	}
84  
85  	public void storePet(Pet pet) {
86  		sessionFactory.getCurrentSession().merge(pet);
87  	}
88  
89  	public void storeVisit(Visit visit) {
90  		sessionFactory.getCurrentSession().merge(visit);
91  	}
92  
93  	public void deletePet(int id) throws DataAccessException {
94  		Pet pet = loadPet(id);
95  		sessionFactory.getCurrentSession().delete(pet);
96  	}
97  
98  }