| 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.List; |
| 19 | |
| 20 | import org.apache.commons.logging.Log; |
| 21 | import org.apache.commons.logging.LogFactory; |
| 22 | import org.hibernate.Session; |
| 23 | import org.hibernate.SessionFactory; |
| 24 | import org.hibernate.context.spi.CurrentSessionContext; |
| 25 | import org.springframework.batch.item.ItemWriter; |
| 26 | import org.springframework.beans.factory.InitializingBean; |
| 27 | import org.springframework.orm.hibernate3.HibernateOperations; |
| 28 | import org.springframework.util.Assert; |
| 29 | |
| 30 | /** |
| 31 | * {@link ItemWriter} that uses a Hibernate session to save or update entities |
| 32 | * that are not part of the current Hibernate session. It will also flush the |
| 33 | * session after writing (i.e. at chunk boundaries if used in a Spring Batch |
| 34 | * TaskletStep). It will also clear the session on write |
| 35 | * default (see {@link #setClearSession(boolean) clearSession} property).<br/> |
| 36 | * <br/> |
| 37 | * |
| 38 | * The writer is thread safe once properties are set (normal singleton behavior) |
| 39 | * if a {@link CurrentSessionContext} that uses only one session per thread is |
| 40 | * used. |
| 41 | * |
| 42 | * @author Dave Syer |
| 43 | * @author Thomas Risberg |
| 44 | * @author Michael Minella |
| 45 | * |
| 46 | */ |
| 47 | public class HibernateItemWriter<T> implements ItemWriter<T>, InitializingBean { |
| 48 | |
| 49 | protected static final Log logger = LogFactory |
| 50 | .getLog(HibernateItemWriter.class); |
| 51 | |
| 52 | private HibernateOperations hibernateTemplate; |
| 53 | private SessionFactory sessionFactory; |
| 54 | |
| 55 | private boolean clearSession = true; |
| 56 | |
| 57 | /** |
| 58 | * Flag to indicate that the session should be cleared and flushed at the |
| 59 | * end of the write (default true). |
| 60 | * |
| 61 | * @param clearSession |
| 62 | * the flag value to set |
| 63 | */ |
| 64 | public void setClearSession(boolean clearSession) { |
| 65 | this.clearSession = clearSession; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Public setter for the {@link HibernateOperations} property. |
| 70 | * |
| 71 | * @param hibernateTemplate |
| 72 | * the hibernateTemplate to set |
| 73 | * @deprecated As of 2.2 in favor of using Hibernate's session management APIs directly |
| 74 | */ |
| 75 | public void setHibernateTemplate(HibernateOperations hibernateTemplate) { |
| 76 | this.hibernateTemplate = hibernateTemplate; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Set the Hibernate SessionFactory to be used internally. |
| 81 | * |
| 82 | * @param sessionFactory session factory to be used by the writer |
| 83 | */ |
| 84 | public void setSessionFactory(SessionFactory sessionFactory) { |
| 85 | this.sessionFactory = sessionFactory; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Check mandatory properties - there must be a hibernateTemplate. |
| 90 | */ |
| 91 | @Override |
| 92 | public void afterPropertiesSet() { |
| 93 | Assert.state(!(hibernateTemplate == null && sessionFactory == null), |
| 94 | "Either HibernateOperations or SessionFactory must be provided"); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Save or update any entities not in the current hibernate session and then |
| 99 | * flush the hibernate session. |
| 100 | * |
| 101 | * @see org.springframework.batch.item.ItemWriter#write(java.util.List) |
| 102 | */ |
| 103 | @Override |
| 104 | public void write(List<? extends T> items) { |
| 105 | if(sessionFactory == null) { |
| 106 | doWrite(hibernateTemplate, items); |
| 107 | hibernateTemplate.flush(); |
| 108 | if (clearSession) { |
| 109 | hibernateTemplate.clear(); |
| 110 | } |
| 111 | } |
| 112 | else { |
| 113 | doWrite(sessionFactory, items); |
| 114 | sessionFactory.getCurrentSession().flush(); |
| 115 | if(clearSession) { |
| 116 | sessionFactory.getCurrentSession().clear(); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Do perform the actual write operation using Hibernate's API. |
| 123 | * This can be overridden in a subclass if necessary. |
| 124 | * |
| 125 | * @param items |
| 126 | * the list of items to use for the write |
| 127 | */ |
| 128 | protected void doWrite(SessionFactory sessionFactory, List<? extends T> items) { |
| 129 | if (logger.isDebugEnabled()) { |
| 130 | logger.debug("Writing to Hibernate with " + items.size() |
| 131 | + " items."); |
| 132 | } |
| 133 | |
| 134 | Session currentSession = sessionFactory.getCurrentSession(); |
| 135 | |
| 136 | if (!items.isEmpty()) { |
| 137 | long saveOrUpdateCount = 0; |
| 138 | for (T item : items) { |
| 139 | if (!currentSession.contains(item)) { |
| 140 | currentSession.saveOrUpdate(item); |
| 141 | saveOrUpdateCount++; |
| 142 | } |
| 143 | } |
| 144 | if (logger.isDebugEnabled()) { |
| 145 | logger.debug(saveOrUpdateCount + " entities saved/updated."); |
| 146 | logger.debug((items.size() - saveOrUpdateCount) |
| 147 | + " entities found in session."); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Do perform the actual write operation using {@link HibernateOperations}. |
| 154 | * This can be overridden in a subclass if necessary. |
| 155 | * |
| 156 | * @param hibernateTemplate |
| 157 | * the HibernateTemplate to use for the operation |
| 158 | * @param items |
| 159 | * the list of items to use for the write |
| 160 | * @deprecated As of 2.2 in favor of using Hibernate's session management APIs directly |
| 161 | */ |
| 162 | protected void doWrite(HibernateOperations hibernateTemplate, |
| 163 | List<? extends T> items) { |
| 164 | |
| 165 | if (logger.isDebugEnabled()) { |
| 166 | logger.debug("Writing to Hibernate with " + items.size() |
| 167 | + " items."); |
| 168 | } |
| 169 | |
| 170 | if (!items.isEmpty()) { |
| 171 | long saveOrUpdateCount = 0; |
| 172 | for (T item : items) { |
| 173 | if (!hibernateTemplate.contains(item)) { |
| 174 | hibernateTemplate.saveOrUpdate(item); |
| 175 | saveOrUpdateCount++; |
| 176 | } |
| 177 | } |
| 178 | if (logger.isDebugEnabled()) { |
| 179 | logger.debug(saveOrUpdateCount + " entities saved/updated."); |
| 180 | logger.debug((items.size() - saveOrUpdateCount) |
| 181 | + " entities found in session."); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | } |
| 186 | |
| 187 | } |