01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.sample.petclinic.hibernate;
18:
19: import java.util.Collection;
20:
21: import org.compass.sample.petclinic.Clinic;
22: import org.compass.sample.petclinic.Owner;
23: import org.compass.sample.petclinic.Pet;
24: import org.compass.sample.petclinic.Visit;
25: import org.springframework.dao.DataAccessException;
26: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
27:
28: /**
29: * Hibernate implementation of the Clinic interface.
30: *
31: * <p>
32: * The mappings are defined in "petclinic.hbm.xml", located in the root of the
33: * class path.
34: *
35: * @author Juergen Hoeller
36: * @since 19.10.2003
37: */
38: public class HibernateClinic extends HibernateDaoSupport implements
39: Clinic {
40:
41: public Collection getPets() throws DataAccessException {
42: return getHibernateTemplate().find(
43: "from Pet pet order by pet.name");
44: }
45:
46: public Collection getVets() throws DataAccessException {
47: return getHibernateTemplate().find(
48: "from Vet vet order by vet.lastName, vet.firstName");
49: }
50:
51: public Collection getPetTypes() throws DataAccessException {
52: return getHibernateTemplate().find(
53: "from PetType type order by type.name");
54: }
55:
56: public Collection findOwners(String lastName)
57: throws DataAccessException {
58: return getHibernateTemplate().find(
59: "from Owner owner where owner.lastName like ?",
60: lastName + "%");
61: }
62:
63: public Owner loadOwner(int id) throws DataAccessException {
64: return (Owner) getHibernateTemplate().load(Owner.class,
65: new Integer(id));
66: }
67:
68: public Pet loadPet(int id) throws DataAccessException {
69: return (Pet) getHibernateTemplate().load(Pet.class,
70: new Integer(id));
71: }
72:
73: public void storeOwner(Owner owner) throws DataAccessException {
74: // Note: Hibernate3's merge operation does not reassociate the object
75: // with the
76: // current Hibernate Session. Instead, it will always copy the state
77: // over to
78: // a registered representation of the entity. In case of a new entity,
79: // it will
80: // register a copy as well, but will not update the id of the passed-in
81: // object.
82: // To still update the ids of the original objects too, we need to
83: // register
84: // Spring's IdTransferringMergeEventListener on our SessionFactory.
85: getHibernateTemplate().merge(owner);
86: }
87:
88: public void storePet(Pet pet) throws DataAccessException {
89: getHibernateTemplate().merge(pet);
90: }
91:
92: public void storeVisit(Visit visit) throws DataAccessException {
93: getHibernateTemplate().merge(visit);
94: }
95:
96: }
|