KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > hibernate3 > support > HibernateDaoSupport


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.springframework.orm.hibernate3.support;
18
19 import org.hibernate.HibernateException;
20 import org.hibernate.Session;
21 import org.hibernate.SessionFactory;
22
23 import org.springframework.dao.DataAccessException;
24 import org.springframework.dao.DataAccessResourceFailureException;
25 import org.springframework.dao.support.DaoSupport;
26 import org.springframework.orm.hibernate3.HibernateTemplate;
27 import org.springframework.orm.hibernate3.SessionFactoryUtils;
28
29 /**
30  * Convenient super class for Hibernate-based data access objects.
31  *
32  * <p>Requires a {@link org.hibernate.SessionFactory} to be set, providing a
33  * {@link org.springframework.orm.hibernate3.HibernateTemplate} based on it to
34  * subclasses through the {@link #getHibernateTemplate()} method.
35  * Can alternatively be initialized directly with a HibernateTemplate,
36  * in order to reuse the latter's settings such as the SessionFactory,
37  * exception translator, flush mode, etc.
38  *
39  * <p>This base class is mainly intended for HibernateTemplate usage but can
40  * also be used when working with a Hibernate Session directly, for example
41  * when relying on transactional Sessions. Convenience {@link #getSession}
42  * and {@link #releaseSession} methods are provided for that usage style.
43  *
44  * <p>This class will create its own HibernateTemplate instance if a SessionFactory
45  * is passed in. The "allowCreate" flag on that HibernateTemplate will be "true"
46  * by default. A custom HibernateTemplate instance can be used through overriding
47  * {@link #createHibernateTemplate}.
48  *
49  * @author Juergen Hoeller
50  * @since 1.2
51  * @see #setSessionFactory
52  * @see #getHibernateTemplate
53  * @see org.springframework.orm.hibernate3.HibernateTemplate
54  */

55 public abstract class HibernateDaoSupport extends DaoSupport {
56
57     private HibernateTemplate hibernateTemplate;
58
59
60     /**
61      * Set the Hibernate SessionFactory to be used by this DAO.
62      * Will automatically create a HibernateTemplate for the given SessionFactory.
63      * @see #createHibernateTemplate
64      * @see #setHibernateTemplate
65      */

66     public final void setSessionFactory(SessionFactory sessionFactory) {
67       this.hibernateTemplate = createHibernateTemplate(sessionFactory);
68     }
69
70     /**
71      * Create a HibernateTemplate for the given SessionFactory.
72      * Only invoked if populating the DAO with a SessionFactory reference!
73      * <p>Can be overridden in subclasses to provide a HibernateTemplate instance
74      * with different configuration, or a custom HibernateTemplate subclass.
75      * @param sessionFactory the Hibernate SessionFactory to create a HibernateTemplate for
76      * @return the new HibernateTemplate instance
77      * @see #setSessionFactory
78      */

79     protected HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory) {
80         return new HibernateTemplate(sessionFactory);
81     }
82
83     /**
84      * Return the Hibernate SessionFactory used by this DAO.
85      */

86     public final SessionFactory getSessionFactory() {
87         return (this.hibernateTemplate != null ? this.hibernateTemplate.getSessionFactory() : null);
88     }
89
90     /**
91      * Set the HibernateTemplate for this DAO explicitly,
92      * as an alternative to specifying a SessionFactory.
93      * @see #setSessionFactory
94      */

95     public final void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
96         this.hibernateTemplate = hibernateTemplate;
97     }
98
99     /**
100      * Return the HibernateTemplate for this DAO,
101      * pre-initialized with the SessionFactory or set explicitly.
102      * <p><b>Note: The returned HibernateTemplate is a shared instance.</b>
103      * You may introspect its configuration, but not modify the configuration
104      * (other than from within an {@link #initDao} implementation).
105      * Consider creating a custom HibernateTemplate instance via
106      * <code>new HibernateTemplate(getSessionFactory())</code>, in which
107      * case you're allowed to customize the settings on the resulting instance.
108      */

109     public final HibernateTemplate getHibernateTemplate() {
110       return this.hibernateTemplate;
111     }
112
113     protected final void checkDaoConfig() {
114         if (this.hibernateTemplate == null) {
115             throw new IllegalArgumentException JavaDoc("'sessionFactory' or 'hibernateTemplate' is required");
116         }
117     }
118
119
120     /**
121      * Obtain a Hibernate Session, either from the current transaction or
122      * a new one. The latter is only allowed if the
123      * {@link org.springframework.orm.hibernate3.HibernateTemplate#setAllowCreate "allowCreate"}
124      * setting of this bean's {@link #setHibernateTemplate HibernateTemplate} is "true".
125      * <p><b>Note that this is not meant to be invoked from HibernateTemplate code
126      * but rather just in plain Hibernate code.</b> Either rely on a thread-bound
127      * Session or use it in combination with {@link #releaseSession}.
128      * <p>In general, it is recommended to use HibernateTemplate, either with
129      * the provided convenience operations or with a custom HibernateCallback
130      * that provides you with a Session to work on. HibernateTemplate will care
131      * for all resource management and for proper exception conversion.
132      * @return the Hibernate Session
133      * @throws DataAccessResourceFailureException if the Session couldn't be created
134      * @throws IllegalStateException if no thread-bound Session found and allowCreate=false
135      * @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession(SessionFactory, boolean)
136      */

137     protected final Session getSession()
138         throws DataAccessResourceFailureException, IllegalStateException JavaDoc {
139
140         return getSession(this.hibernateTemplate.isAllowCreate());
141     }
142
143     /**
144      * Obtain a Hibernate Session, either from the current transaction or
145      * a new one. The latter is only allowed if "allowCreate" is true.
146      * <p><b>Note that this is not meant to be invoked from HibernateTemplate code
147      * but rather just in plain Hibernate code.</b> Either rely on a thread-bound
148      * Session or use it in combination with {@link #releaseSession}.
149      * <p>In general, it is recommended to use
150      * {@link #getHibernateTemplate() HibernateTemplate}, either with
151      * the provided convenience operations or with a custom
152      * {@link org.springframework.orm.hibernate3.HibernateCallback} that
153      * provides you with a Session to work on. HibernateTemplate will care
154      * for all resource management and for proper exception conversion.
155      * @param allowCreate if a non-transactional Session should be created when no
156      * transactional Session can be found for the current thread
157      * @return the Hibernate Session
158      * @throws DataAccessResourceFailureException if the Session couldn't be created
159      * @throws IllegalStateException if no thread-bound Session found and allowCreate=false
160      * @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession(SessionFactory, boolean)
161      */

162     protected final Session getSession(boolean allowCreate)
163         throws DataAccessResourceFailureException, IllegalStateException JavaDoc {
164
165         return (!allowCreate ?
166             SessionFactoryUtils.getSession(getSessionFactory(), false) :
167                 SessionFactoryUtils.getSession(
168                         getSessionFactory(),
169                         this.hibernateTemplate.getEntityInterceptor(),
170                         this.hibernateTemplate.getJdbcExceptionTranslator()));
171     }
172
173     /**
174      * Convert the given HibernateException to an appropriate exception from the
175      * <code>org.springframework.dao</code> hierarchy. Will automatically detect
176      * wrapped SQLExceptions and convert them accordingly.
177      * <p>Delegates to the
178      * {@link org.springframework.orm.hibernate3.HibernateTemplate#convertHibernateAccessException}
179      * method of this DAO's HibernateTemplate.
180      * <p>Typically used in plain Hibernate code, in combination with
181      * {@link #getSession} and {@link #releaseSession}.
182      * @param ex HibernateException that occured
183      * @return the corresponding DataAccessException instance
184      * @see org.springframework.orm.hibernate3.SessionFactoryUtils#convertHibernateAccessException
185      */

186     protected final DataAccessException convertHibernateAccessException(HibernateException ex) {
187         return this.hibernateTemplate.convertHibernateAccessException(ex);
188     }
189
190     /**
191      * Close the given Hibernate Session, created via this DAO's SessionFactory,
192      * if it isn't bound to the thread (i.e. isn't a transactional Session).
193      * <p>Typically used in plain Hibernate code, in combination with
194      * {@link #getSession} and {@link #convertHibernateAccessException}.
195      * @param session the Session to close
196      * @see org.springframework.orm.hibernate3.SessionFactoryUtils#releaseSession
197      */

198     protected final void releaseSession(Session session) {
199         SessionFactoryUtils.releaseSession(session, getSessionFactory());
200     }
201
202 }
203
Popular Tags