EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.item.database]

COVERAGE SUMMARY FOR SOURCE FILE [HibernateCursorItemReader.java]

nameclass, %method, %block, %line, %
HibernateCursorItemReader.java100% (1/1)100% (14/14)99%  (137/138)100% (44.9/45)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class HibernateCursorItemReader100% (1/1)100% (14/14)99%  (137/138)100% (44.9/45)
afterPropertiesSet (): void 100% (1/1)92%  (11/12)97%  (2.9/3)
HibernateCursorItemReader (): void 100% (1/1)100% (15/15)100% (5/5)
doClose (): void 100% (1/1)100% (13/13)100% (5/5)
doOpen (): void 100% (1/1)100% (21/21)100% (4/4)
doRead (): Object 100% (1/1)100% (24/24)100% (8/8)
jumpToItem (int): void 100% (1/1)100% (13/13)100% (3/3)
setFetchSize (int): void 100% (1/1)100% (4/4)100% (2/2)
setParameterValues (Map): void 100% (1/1)100% (4/4)100% (2/2)
setQueryName (String): void 100% (1/1)100% (5/5)100% (2/2)
setQueryProvider (HibernateQueryProvider): void 100% (1/1)100% (5/5)100% (2/2)
setQueryString (String): void 100% (1/1)100% (5/5)100% (2/2)
setSessionFactory (SessionFactory): void 100% (1/1)100% (5/5)100% (2/2)
setUseStatelessSession (boolean): void 100% (1/1)100% (5/5)100% (2/2)
update (ExecutionContext): void 100% (1/1)100% (7/7)100% (3/3)

1/*
2 * Copyright 2006-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 */
16package org.springframework.batch.item.database;
17 
18import java.util.Map;
19 
20import org.hibernate.ScrollableResults;
21import org.hibernate.Session;
22import org.hibernate.SessionFactory;
23import org.hibernate.StatelessSession;
24import org.springframework.batch.item.ExecutionContext;
25import org.springframework.batch.item.ItemReader;
26import org.springframework.batch.item.ItemStream;
27import org.springframework.batch.item.ItemStreamException;
28import org.springframework.batch.item.database.orm.HibernateQueryProvider;
29import org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader;
30import org.springframework.beans.factory.InitializingBean;
31import org.springframework.util.Assert;
32import org.springframework.util.ClassUtils;
33 
34/**
35 * {@link ItemReader} for reading database records built on top of Hibernate. It
36 * executes the HQL query when initialized iterates over the result set as
37 * {@link #read()} method is called, returning an object corresponding to
38 * current row. The query can be set directly using
39 * {@link #setQueryString(String)}, a named query can be used by
40 * {@link #setQueryName(String)}, or a query provider strategy can be supplied
41 * via {@link #setQueryProvider(HibernateQueryProvider)}.
42 * 
43 * 
44 * <p>
45 * The reader can be configured to use either {@link StatelessSession}
46 * sufficient for simple mappings without the need to cascade to associated
47 * objects or standard hibernate {@link Session} for more advanced mappings or
48 * when caching is desired. When stateful session is used it will be cleared in
49 * the {@link #update(ExecutionContext)} method without being flushed (no data
50 * modifications are expected).
51 * </p>
52 * 
53 * The implementation is <b>not</b> thread-safe.
54 * 
55 * @author Robert Kasanicky
56 * @author Dave Syer
57 */
58public class HibernateCursorItemReader<T> extends AbstractItemCountingItemStreamItemReader<T> implements ItemStream,
59                InitializingBean {
60 
61        private HibernateItemReaderHelper<T> helper = new HibernateItemReaderHelper<T>();
62 
63        public HibernateCursorItemReader() {
64                setName(ClassUtils.getShortName(HibernateCursorItemReader.class));
65        }
66 
67        private ScrollableResults cursor;
68 
69        private boolean initialized = false;
70 
71        private int fetchSize;
72 
73        private Map<String, Object> parameterValues;
74 
75        public void afterPropertiesSet() throws Exception {
76                Assert.state(fetchSize >= 0, "fetchSize must not be negative");
77                helper.afterPropertiesSet();
78        }
79 
80        /**
81         * The parameter values to apply to a query (map of name:value).
82         * 
83         * @param parameterValues the parameter values to set
84         */
85        public void setParameterValues(Map<String, Object> parameterValues) {
86                this.parameterValues = parameterValues;
87        }
88 
89        /**
90         * A query name for an externalized query. Either this or the {
91         * {@link #setQueryString(String) query string} or the {
92         * {@link #setQueryProvider(HibernateQueryProvider) query provider} should
93         * be set.
94         * 
95         * @param queryName name of a hibernate named query
96         */
97        public void setQueryName(String queryName) {
98                helper.setQueryName(queryName);
99        }
100 
101        /**
102         * Fetch size used internally by Hibernate to limit amount of data fetched
103         * from database per round trip.
104         * 
105         * @param fetchSize the fetch size to pass down to Hibernate
106         */
107        public void setFetchSize(int fetchSize) {
108                this.fetchSize = fetchSize;
109        }
110 
111        /**
112         * A query provider. Either this or the {{@link #setQueryString(String)
113         * query string} or the {{@link #setQueryName(String) query name} should be
114         * set.
115         * 
116         * @param queryProvider Hibernate query provider
117         */
118        public void setQueryProvider(HibernateQueryProvider queryProvider) {
119                helper.setQueryProvider(queryProvider);
120        }
121 
122        /**
123         * A query string in HQL. Either this or the {
124         * {@link #setQueryProvider(HibernateQueryProvider) query provider} or the {
125         * {@link #setQueryName(String) query name} should be set.
126         * 
127         * @param queryString HQL query string
128         */
129        public void setQueryString(String queryString) {
130                helper.setQueryString(queryString);
131        }
132 
133        /**
134         * The Hibernate SessionFactory to use the create a session.
135         * 
136         * @param sessionFactory the {@link SessionFactory} to set
137         */
138        public void setSessionFactory(SessionFactory sessionFactory) {
139                helper.setSessionFactory(sessionFactory);
140        }
141 
142        /**
143         * Can be set only in uninitialized state.
144         * 
145         * @param useStatelessSession <code>true</code> to use
146         * {@link StatelessSession} <code>false</code> to use standard hibernate
147         * {@link Session}
148         */
149        public void setUseStatelessSession(boolean useStatelessSession) {
150                helper.setUseStatelessSession(useStatelessSession);
151        }
152 
153        protected T doRead() throws Exception {
154                if (cursor.next()) {
155                        Object[] data = cursor.get();
156 
157                        if (data.length > 1) {
158                                // If there are multiple items this must be a projection
159                                // and T is an array type.
160                                @SuppressWarnings("unchecked")
161                                T item = (T) data;
162                                return item;
163                        }
164                        else {
165                                // Assume if there is only one item that it is the data the user
166                                // wants.
167                                // If there is only one item this is going to be a nasty shock
168                                // if T is an array type but there's not much else we can do...
169                                @SuppressWarnings("unchecked")
170                                T item = (T) data[0];
171                                return item;
172                        }
173 
174                }
175                return null;
176        }
177 
178        /**
179         * Open hibernate session and create a forward-only cursor for the query.
180         */
181        protected void doOpen() throws Exception {
182                Assert.state(!initialized, "Cannot open an already opened ItemReader, call close first");
183                cursor = helper.getForwardOnlyCursor(fetchSize, parameterValues);
184                initialized = true;
185        }
186 
187        /**
188         * Update the context and clear the session if stateful.
189         * 
190         * @param executionContext the current {@link ExecutionContext}
191         * @throws ItemStreamException if there is a problem
192         */
193        @Override
194        public void update(ExecutionContext executionContext) throws ItemStreamException {
195                super.update(executionContext);
196                helper.clear();
197        }
198 
199        /**
200         * Wind forward through the result set to the item requested. Also clears
201         * the session every now and then (if stateful) to avoid memory problems.
202         * The frequency of session clearing is the larger of the fetch size (if
203         * set) and 100.
204         * 
205         * @param itemIndex the first item to read
206         * @throws Exception if there is a problem
207         * @see AbstractItemCountingItemStreamItemReader#jumpToItem(int)
208         */
209        @Override
210        protected void jumpToItem(int itemIndex) throws Exception {
211                int flushSize = Math.max(fetchSize, 100);
212                helper.jumpToItem(cursor, itemIndex, flushSize);
213        }
214 
215        /**
216         * Close the cursor and hibernate session.
217         */
218        protected void doClose() throws Exception {
219 
220                initialized = false;
221 
222                if (cursor != null) {
223                        cursor.close();
224                }
225 
226                helper.close();
227 
228        }
229}

[all classes][org.springframework.batch.item.database]
EMMA 2.0.5312 (C) Vladimir Roubtsov