Skip to content

Commit 098fdec

Browse files
committed
Batch inserts (including associations)
1 parent 2bf39a5 commit 098fdec

File tree

2 files changed

+5
-19
lines changed

2 files changed

+5
-19
lines changed

HibernateSpringBootBatchInsertOrder/src/main/java/com/bookstore/impl/BatchRepository.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
@NoRepositoryBean
88
public interface BatchRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
9-
10-
<S extends T> S persist(S entity);
11-
<S extends T> Iterable<S> saveInBatch(Iterable<S> entites);
9+
10+
<S extends T> void saveInBatch(Iterable<S> entites);
1211
}

HibernateSpringBootBatchInsertOrder/src/main/java/com/bookstore/impl/BatchRepositoryImpl.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import java.io.IOException;
44
import java.io.InputStream;
55
import java.io.Serializable;
6-
import java.util.ArrayList;
7-
import java.util.List;
86
import java.util.Properties;
97
import java.util.logging.Level;
108
import java.util.logging.Logger;
@@ -31,24 +29,15 @@ public BatchRepositoryImpl(JpaEntityInformation entityInformation,
3129

3230
@Override
3331
@Transactional
34-
public <S extends T> S persist(S entity) {
35-
entityManager.persist(entity);
36-
37-
return entity;
38-
}
39-
40-
@Override
41-
@Transactional
42-
public <S extends T> Iterable<S> saveInBatch(Iterable<S> entities) {
32+
public <S extends T> void saveInBatch(Iterable<S> entities) {
4333

4434
if (entities == null) {
4535
throw new IllegalArgumentException("The given Iterable of entities cannot be null!");
4636
}
4737

4838
int i = 0;
49-
List<S> result = new ArrayList<>();
5039
for (S entity : entities) {
51-
result.add(persist(entity));
40+
entityManager.persist(entity);
5241

5342
i++;
5443

@@ -70,16 +59,14 @@ public <S extends T> Iterable<S> saveInBatch(Iterable<S> entities) {
7059
entityManager.flush();
7160
entityManager.clear();
7261
}
73-
74-
return result;
7562
}
7663

7764
private static int batchSize() {
7865

7966
int batchsize = Integer.valueOf(Dialect.DEFAULT_BATCH_SIZE); // default batch size
8067

8168
Properties configuration = new Properties();
82-
try ( InputStream inputStream = BatchRepositoryImpl.class.getClassLoader()
69+
try (InputStream inputStream = BatchRepositoryImpl.class.getClassLoader()
8370
.getResourceAsStream("application.properties")) {
8471
configuration.load(inputStream);
8572
} catch (IOException ex) {

0 commit comments

Comments
 (0)