1 | /* |
2 | * Copyright 2006-2008 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 | |
17 | package org.springframework.batch.item.database; |
18 | |
19 | import java.util.List; |
20 | |
21 | import javax.persistence.EntityManager; |
22 | import javax.persistence.EntityManagerFactory; |
23 | |
24 | import org.apache.commons.logging.Log; |
25 | import org.apache.commons.logging.LogFactory; |
26 | import org.springframework.batch.item.ItemWriter; |
27 | import org.springframework.beans.factory.InitializingBean; |
28 | import org.springframework.dao.DataAccessResourceFailureException; |
29 | import org.springframework.orm.jpa.EntityManagerFactoryUtils; |
30 | import org.springframework.util.Assert; |
31 | |
32 | /** |
33 | * {@link org.springframework.batch.item.ItemWriter} that is using a JPA |
34 | * EntityManagerFactory to merge any Entities that aren't part of the |
35 | * persistence context. |
36 | * |
37 | * It is required that {@link #write(List)} is called inside a transaction.<br/> |
38 | * |
39 | * The reader must be configured with an |
40 | * {@link javax.persistence.EntityManagerFactory} that is capable of |
41 | * participating in Spring managed transactions. |
42 | * |
43 | * The writer is thread safe after its properties are set (normal singleton |
44 | * behaviour), so it can be used to write in multiple concurrent transactions. |
45 | * |
46 | * @author Thomas Risberg |
47 | * |
48 | */ |
49 | public class JpaItemWriter<T> implements ItemWriter<T>, InitializingBean { |
50 | |
51 | protected static final Log logger = LogFactory.getLog(JpaItemWriter.class); |
52 | |
53 | private EntityManagerFactory entityManagerFactory; |
54 | |
55 | /** |
56 | * Set the EntityManager to be used internally. |
57 | * |
58 | * @param entityManagerFactory the entityManagerFactory to set |
59 | */ |
60 | public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) { |
61 | this.entityManagerFactory = entityManagerFactory; |
62 | } |
63 | |
64 | /** |
65 | * Check mandatory properties - there must be an entityManagerFactory. |
66 | */ |
67 | public void afterPropertiesSet() throws Exception { |
68 | Assert.notNull(entityManagerFactory, "An EntityManagerFactory is required"); |
69 | } |
70 | |
71 | /** |
72 | * Merge all provided items that aren't already in the persistence context |
73 | * and then flush the entity manager. |
74 | * |
75 | * @see org.springframework.batch.item.ItemWriter#write(java.util.List) |
76 | */ |
77 | public final void write(List<? extends T> items) { |
78 | EntityManager entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory); |
79 | if (entityManager == null) { |
80 | throw new DataAccessResourceFailureException("Unable to obtain a transactional EntityManager"); |
81 | } |
82 | doWrite(entityManager, items); |
83 | entityManager.flush(); |
84 | } |
85 | |
86 | /** |
87 | * Do perform the actual write operation. This can be overridden in a |
88 | * subclass if necessary. |
89 | * |
90 | * @param entityManager the EntityManager to use for the operation |
91 | * @param items the list of items to use for the write |
92 | */ |
93 | protected void doWrite(EntityManager entityManager, List<? extends T> items) { |
94 | |
95 | if (logger.isDebugEnabled()) { |
96 | logger.debug("Writing to JPA with " + items.size() + " items."); |
97 | } |
98 | |
99 | if (!items.isEmpty()) { |
100 | long mergeCount = 0; |
101 | for (T item : items) { |
102 | if (!entityManager.contains(item)) { |
103 | entityManager.merge(item); |
104 | mergeCount++; |
105 | } |
106 | } |
107 | if (logger.isDebugEnabled()) { |
108 | logger.debug(mergeCount + " entities merged."); |
109 | logger.debug((items.size() - mergeCount) + " entities found in persistence context."); |
110 | } |
111 | } |
112 | |
113 | } |
114 | |
115 | } |