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 | */ |
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.SessionFactory; |
23 | import org.springframework.batch.item.ItemWriter; |
24 | import org.springframework.beans.factory.InitializingBean; |
25 | import org.springframework.orm.hibernate3.HibernateOperations; |
26 | import org.springframework.orm.hibernate3.HibernateTemplate; |
27 | import org.springframework.util.Assert; |
28 | |
29 | /** |
30 | * {@link ItemWriter} that uses a Hibernate session to save or update entities |
31 | * that are not part of the current Hibernate session. It will also flush the |
32 | * session after writing (i.e. at chunk boundaries if used in a Spring Batch |
33 | * TaskletStep). It will also clear the session on write |
34 | * default (see {@link #setClearSession(boolean) clearSession} property).<br/> |
35 | * <br/> |
36 | * |
37 | * The writer is thread safe after its properties are set (normal singleton |
38 | * behavior), so it can be used to write in multiple concurrent transactions. |
39 | * |
40 | * @author Dave Syer |
41 | * @author Thomas Risberg |
42 | * |
43 | */ |
44 | public class HibernateItemWriter<T> implements ItemWriter<T>, InitializingBean { |
45 | |
46 | protected static final Log logger = LogFactory |
47 | .getLog(HibernateItemWriter.class); |
48 | |
49 | private HibernateOperations hibernateTemplate; |
50 | |
51 | private boolean clearSession = true; |
52 | |
53 | /** |
54 | * Flag to indicate that the session should be cleared and flushed at the |
55 | * end of the write (default true). |
56 | * |
57 | * @param clearSession |
58 | * the flag value to set |
59 | */ |
60 | public void setClearSession(boolean clearSession) { |
61 | this.clearSession = clearSession; |
62 | } |
63 | |
64 | /** |
65 | * Public setter for the {@link HibernateOperations} property. |
66 | * |
67 | * @param hibernateTemplate |
68 | * the hibernateTemplate to set |
69 | */ |
70 | public void setHibernateTemplate(HibernateOperations hibernateTemplate) { |
71 | this.hibernateTemplate = hibernateTemplate; |
72 | } |
73 | |
74 | /** |
75 | * Set the Hibernate SessionFactory to be used internally. Will |
76 | * automatically create a HibernateTemplate for the given SessionFactory. |
77 | * |
78 | * @see #setHibernateTemplate |
79 | */ |
80 | public final void setSessionFactory(SessionFactory sessionFactory) { |
81 | this.hibernateTemplate = new HibernateTemplate(sessionFactory); |
82 | } |
83 | |
84 | /** |
85 | * Check mandatory properties - there must be a hibernateTemplate. |
86 | */ |
87 | public void afterPropertiesSet() { |
88 | Assert.notNull(hibernateTemplate, |
89 | "HibernateItemWriter requires a HibernateOperations"); |
90 | } |
91 | |
92 | /** |
93 | * Save or update any entities not in the current hibernate session and then |
94 | * flush the hibernate session. |
95 | * |
96 | * @see org.springframework.batch.item.ItemWriter#write(java.util.List) |
97 | */ |
98 | public final void write(List<? extends T> items) { |
99 | doWrite(hibernateTemplate, items); |
100 | hibernateTemplate.flush(); |
101 | if (clearSession) { |
102 | hibernateTemplate.clear(); |
103 | } |
104 | } |
105 | |
106 | /** |
107 | * Do perform the actual write operation. This can be overridden in a |
108 | * subclass if necessary. |
109 | * |
110 | * @param hibernateTemplate |
111 | * the HibernateTemplate to use for the operation |
112 | * @param items |
113 | * the list of items to use for the write |
114 | */ |
115 | protected void doWrite(HibernateOperations hibernateTemplate, |
116 | List<? extends T> items) { |
117 | |
118 | if (logger.isDebugEnabled()) { |
119 | logger.debug("Writing to Hibernate with " + items.size() |
120 | + " items."); |
121 | } |
122 | |
123 | if (!items.isEmpty()) { |
124 | long saveOrUpdateCount = 0; |
125 | for (T item : items) { |
126 | if (!hibernateTemplate.contains(item)) { |
127 | hibernateTemplate.saveOrUpdate(item); |
128 | saveOrUpdateCount++; |
129 | } |
130 | } |
131 | if (logger.isDebugEnabled()) { |
132 | logger.debug(saveOrUpdateCount + " entities saved/updated."); |
133 | logger.debug((items.size() - saveOrUpdateCount) |
134 | + " entities found in session."); |
135 | } |
136 | } |
137 | |
138 | } |
139 | |
140 | } |