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

COVERAGE SUMMARY FOR SOURCE FILE [HibernateItemWriter.java]

nameclass, %method, %block, %line, %
HibernateItemWriter.java100% (1/1)75%  (6/8)90%  (104/115)86%  (24/28)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class HibernateItemWriter100% (1/1)75%  (6/8)90%  (104/115)86%  (24/28)
setClearSession (boolean): void 0%   (0/1)0%   (0/4)0%   (0/2)
setSessionFactory (SessionFactory): void 0%   (0/1)0%   (0/7)0%   (0/2)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
HibernateItemWriter (): void 100% (1/1)100% (6/6)100% (2/2)
afterPropertiesSet (): void 100% (1/1)100% (5/5)100% (2/2)
doWrite (HibernateOperations, List): void 100% (1/1)100% (70/70)100% (12/12)
setHibernateTemplate (HibernateOperations): void 100% (1/1)100% (4/4)100% (2/2)
write (List): void 100% (1/1)100% (15/15)100% (5/5)

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.List;
19 
20import org.apache.commons.logging.Log;
21import org.apache.commons.logging.LogFactory;
22import org.hibernate.SessionFactory;
23import org.springframework.batch.item.ItemWriter;
24import org.springframework.beans.factory.InitializingBean;
25import org.springframework.orm.hibernate3.HibernateOperations;
26import org.springframework.orm.hibernate3.HibernateTemplate;
27import 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 */
44public 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}

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