| 1 | /* |
| 2 | * Copyright 2006-2013 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 | package org.springframework.batch.item.database; |
| 17 | |
| 18 | import java.util.Map; |
| 19 | import java.util.concurrent.CopyOnWriteArrayList; |
| 20 | |
| 21 | import org.hibernate.Session; |
| 22 | import org.hibernate.SessionFactory; |
| 23 | import org.hibernate.StatelessSession; |
| 24 | import org.springframework.batch.item.ExecutionContext; |
| 25 | import org.springframework.batch.item.ItemReader; |
| 26 | import org.springframework.batch.item.ItemStream; |
| 27 | import org.springframework.batch.item.database.orm.HibernateQueryProvider; |
| 28 | import org.springframework.beans.factory.InitializingBean; |
| 29 | import org.springframework.util.Assert; |
| 30 | import org.springframework.util.ClassUtils; |
| 31 | |
| 32 | /** |
| 33 | * {@link ItemReader} for reading database records built on top of Hibernate and |
| 34 | * reading only up to a fixed number of items at a time. It executes an HQL |
| 35 | * query when initialized is paged as the {@link #read()} method is called. The |
| 36 | * query can be set directly using {@link #setQueryString(String)}, a named |
| 37 | * query can be used by {@link #setQueryName(String)}, or a query provider |
| 38 | * strategy can be supplied via |
| 39 | * {@link #setQueryProvider(HibernateQueryProvider)}. |
| 40 | * |
| 41 | * <p> |
| 42 | * The reader can be configured to use either {@link StatelessSession} |
| 43 | * sufficient for simple mappings without the need to cascade to associated |
| 44 | * objects or standard hibernate {@link Session} for more advanced mappings or |
| 45 | * when caching is desired. When stateful session is used it will be cleared in |
| 46 | * the {@link #update(ExecutionContext)} method without being flushed (no data |
| 47 | * modifications are expected). |
| 48 | * </p> |
| 49 | * |
| 50 | * <p> |
| 51 | * The implementation is thread-safe in between calls to |
| 52 | * {@link #open(ExecutionContext)}, but remember to use |
| 53 | * <code>saveState=false</code> if used in a multi-threaded client (no restart |
| 54 | * available). |
| 55 | * </p> |
| 56 | * |
| 57 | * @author Dave Syer |
| 58 | * |
| 59 | * @since 2.1 |
| 60 | */ |
| 61 | public class HibernatePagingItemReader<T> extends AbstractPagingItemReader<T> |
| 62 | implements InitializingBean { |
| 63 | |
| 64 | private HibernateItemReaderHelper<T> helper = new HibernateItemReaderHelper<T>(); |
| 65 | |
| 66 | private Map<String, Object> parameterValues; |
| 67 | |
| 68 | private int fetchSize; |
| 69 | |
| 70 | public HibernatePagingItemReader() { |
| 71 | setName(ClassUtils.getShortName(HibernatePagingItemReader.class)); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * The parameter values to apply to a query (map of name:value). |
| 76 | * |
| 77 | * @param parameterValues the parameter values to set |
| 78 | */ |
| 79 | public void setParameterValues(Map<String, Object> parameterValues) { |
| 80 | this.parameterValues = parameterValues; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * A query name for an externalized query. Either this or the { |
| 85 | * {@link #setQueryString(String) query string} or the { |
| 86 | * {@link #setQueryProvider(HibernateQueryProvider) query provider} should |
| 87 | * be set. |
| 88 | * |
| 89 | * @param queryName name of a hibernate named query |
| 90 | */ |
| 91 | public void setQueryName(String queryName) { |
| 92 | helper.setQueryName(queryName); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Fetch size used internally by Hibernate to limit amount of data fetched |
| 97 | * from database per round trip. |
| 98 | * |
| 99 | * @param fetchSize the fetch size to pass down to Hibernate |
| 100 | */ |
| 101 | public void setFetchSize(int fetchSize) { |
| 102 | this.fetchSize = fetchSize; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * A query provider. Either this or the {{@link #setQueryString(String) |
| 107 | * query string} or the {{@link #setQueryName(String) query name} should be |
| 108 | * set. |
| 109 | * |
| 110 | * @param queryProvider Hibernate query provider |
| 111 | */ |
| 112 | public void setQueryProvider(HibernateQueryProvider queryProvider) { |
| 113 | helper.setQueryProvider(queryProvider); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * A query string in HQL. Either this or the { |
| 118 | * {@link #setQueryProvider(HibernateQueryProvider) query provider} or the { |
| 119 | * {@link #setQueryName(String) query name} should be set. |
| 120 | * |
| 121 | * @param queryString HQL query string |
| 122 | */ |
| 123 | public void setQueryString(String queryString) { |
| 124 | helper.setQueryString(queryString); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * The Hibernate SessionFactory to use the create a session. |
| 129 | * |
| 130 | * @param sessionFactory the {@link SessionFactory} to set |
| 131 | */ |
| 132 | public void setSessionFactory(SessionFactory sessionFactory) { |
| 133 | helper.setSessionFactory(sessionFactory); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Can be set only in uninitialized state. |
| 138 | * |
| 139 | * @param useStatelessSession <code>true</code> to use |
| 140 | * {@link StatelessSession} <code>false</code> to use standard hibernate |
| 141 | * {@link Session} |
| 142 | */ |
| 143 | public void setUseStatelessSession(boolean useStatelessSession) { |
| 144 | helper.setUseStatelessSession(useStatelessSession); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void afterPropertiesSet() throws Exception { |
| 149 | super.afterPropertiesSet(); |
| 150 | Assert.state(fetchSize >= 0, "fetchSize must not be negative"); |
| 151 | helper.afterPropertiesSet(); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | protected void doOpen() throws Exception { |
| 156 | super.doOpen(); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | protected void doReadPage() { |
| 161 | |
| 162 | if (results == null) { |
| 163 | results = new CopyOnWriteArrayList<T>(); |
| 164 | } |
| 165 | else { |
| 166 | results.clear(); |
| 167 | } |
| 168 | |
| 169 | results.addAll(helper.readPage(getPage(), getPageSize(), fetchSize, parameterValues)); |
| 170 | |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | protected void doJumpToPage(int itemIndex) { |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | protected void doClose() throws Exception { |
| 179 | helper.close(); |
| 180 | super.doClose(); |
| 181 | } |
| 182 | |
| 183 | } |