KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > petclinic > hibernate > HibernateClinic


1 package org.springframework.samples.petclinic.hibernate;
2
3 import java.util.Collection JavaDoc;
4
5 import org.springframework.dao.DataAccessException;
6 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
7 import org.springframework.samples.petclinic.Clinic;
8 import org.springframework.samples.petclinic.Owner;
9 import org.springframework.samples.petclinic.Pet;
10 import org.springframework.samples.petclinic.Visit;
11
12 /**
13  * Hibernate implementation of the Clinic interface.
14  *
15  * <p>The mappings are defined in "petclinic.hbm.xml",
16  * located in the root of the class path.
17  *
18  * @author Juergen Hoeller
19  * @since 19.10.2003
20  */

21 public class HibernateClinic extends HibernateDaoSupport implements Clinic {
22
23     public Collection JavaDoc getVets() throws DataAccessException {
24         return getHibernateTemplate().find("from Vet vet order by vet.lastName, vet.firstName");
25     }
26
27     public Collection JavaDoc getPetTypes() throws DataAccessException {
28         return getHibernateTemplate().find("from PetType type order by type.name");
29     }
30
31     public Collection JavaDoc findOwners(String JavaDoc lastName) throws DataAccessException {
32         return getHibernateTemplate().find("from Owner owner where owner.lastName like ?", lastName + "%");
33     }
34
35     public Owner loadOwner(int id) throws DataAccessException {
36         return (Owner) getHibernateTemplate().load(Owner.class, new Integer JavaDoc(id));
37     }
38
39     public Pet loadPet(int id) throws DataAccessException {
40         return (Pet) getHibernateTemplate().load(Pet.class, new Integer JavaDoc(id));
41     }
42
43     public void storeOwner(Owner owner) throws DataAccessException {
44         // Note: Hibernate3's merge operation does not reassociate the object with the
45
// current Hibernate Session. Instead, it will always copy the state over to
46
// a registered representation of the entity. In case of a new entity, it will
47
// register a copy as well, but will not update the id of the passed-in object.
48
// To still update the ids of the original objects too, we need to register
49
// Spring's IdTransferringMergeEventListener on our SessionFactory.
50
getHibernateTemplate().merge(owner);
51     }
52
53     public void storePet(Pet pet) throws DataAccessException {
54         getHibernateTemplate().merge(pet);
55     }
56
57     public void storeVisit(Visit visit) throws DataAccessException {
58         getHibernateTemplate().merge(visit);
59     }
60
61 }
62
Popular Tags