Skip to content

Commit 76ff0d2

Browse files
blockchain-trainerjzheaux
authored andcommitted
[BAEL-2718] - Spring JPA Batch Inserts (eugenp#6333)
* [BAEL-2718] - Spring JPA Batch Inserts * [BAEL-2718] - Spring JPA Batch Inserts (New PR) * [BAEL-2718] - Spring JPA Batch Inserts New PR * [BAEL-2718] - Spring JPA Batch Inserts * BAEL-2718 - formatting (tab to space)
1 parent 728e5f3 commit 76ff0d2

File tree

6 files changed

+173
-1
lines changed

6 files changed

+173
-1
lines changed

persistence-modules/spring-data-jpa/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@
5353
<artifactId>spring-security-test</artifactId>
5454
<scope>test</scope>
5555
</dependency>
56+
<dependency>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-starter-web</artifactId>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-starter-test</artifactId>
63+
<scope>test</scope>
64+
</dependency>
5665

5766
<dependency>
5867
<groupId>com.google.guava</groupId>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.batchinserts;
2+
3+
import java.net.URISyntaxException;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.bind.annotation.PostMapping;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import com.baeldung.batchinserts.model.Customer;
13+
import com.baeldung.batchinserts.repository.CustomerRepository;
14+
15+
/**
16+
* A simple controller to test the JPA CrudRepository operations
17+
* controllers
18+
*
19+
* @author ysharma2512
20+
*
21+
*/
22+
@RestController
23+
public class CustomerController {
24+
25+
@Autowired
26+
CustomerRepository customerRepository;
27+
28+
public CustomerController(CustomerRepository customerRepository2) {
29+
this.customerRepository = customerRepository2;
30+
}
31+
32+
@PostMapping("/customers")
33+
public ResponseEntity<List<Customer>> insertCustomers() throws URISyntaxException {
34+
Customer c1 = new Customer("James", "Gosling");
35+
Customer c2 = new Customer("Doug", "Lea");
36+
Customer c3 = new Customer("Martin", "Fowler");
37+
Customer c4 = new Customer("Brian", "Goetz");
38+
List<Customer> customers = Arrays.asList(c1, c2, c3, c4);
39+
customerRepository.saveAll(customers);
40+
return ResponseEntity.ok(customers);
41+
}
42+
43+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.batchinserts.model;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
8+
/**
9+
* Customer Entity class
10+
* @author ysharma2512
11+
*
12+
*/
13+
@Entity
14+
public class Customer {
15+
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.AUTO)
18+
private Long id;
19+
private String firstName;
20+
private String lastName;
21+
22+
public Customer(String firstName, String lastName) {
23+
this.firstName = firstName;
24+
this.lastName = lastName;
25+
}
26+
27+
public Long getId() {
28+
return id;
29+
}
30+
31+
public void setId(Long id) {
32+
this.id = id;
33+
}
34+
35+
public String getFirstName() {
36+
return firstName;
37+
}
38+
39+
public void setFirstName(String firstName) {
40+
this.firstName = firstName;
41+
}
42+
43+
public String getLastName() {
44+
return lastName;
45+
}
46+
47+
public void setLastName(String lastName) {
48+
this.lastName = lastName;
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
54+
}
55+
56+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.batchinserts.repository;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
5+
import com.baeldung.batchinserts.model.Customer;
6+
7+
/**
8+
* JPA CrudRepository interface
9+
*
10+
* @author ysharma2512
11+
*
12+
*/
13+
public interface CustomerRepository extends CrudRepository<Customer, Long>{
14+
15+
}

persistence-modules/spring-data-jpa/src/main/resources/application.properties

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFa
1414

1515
spring.datasource.data=import_entities.sql
1616

17-
spring.main.allow-bean-definition-overriding=true
17+
spring.main.allow-bean-definition-overriding=true
18+
19+
spring.jpa.properties.hibernate.jdbc.batch_size=4
20+
spring.jpa.properties.hibernate.order_inserts=true
21+
spring.jpa.properties.hibernate.order_updates=true
22+
spring.jpa.properties.hibernate.generate_statistics=true
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.batchinserts;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
4+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
5+
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.test.context.ContextConfiguration;
13+
import org.springframework.test.context.junit4.SpringRunner;
14+
import org.springframework.test.web.servlet.MockMvc;
15+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
16+
17+
import com.baeldung.batchinserts.CustomerController;
18+
import com.baeldung.batchinserts.repository.CustomerRepository;
19+
import com.baeldung.config.PersistenceConfiguration;
20+
import com.baeldung.config.PersistenceProductConfiguration;
21+
import com.baeldung.config.PersistenceUserConfiguration;
22+
23+
@RunWith(SpringRunner.class)
24+
@SpringBootTest
25+
@AutoConfigureMockMvc
26+
@ContextConfiguration(classes = { PersistenceConfiguration.class, PersistenceProductConfiguration.class, PersistenceUserConfiguration.class })
27+
public class BatchInsertIntegrationTest {
28+
29+
@Autowired
30+
private CustomerRepository customerRepository;
31+
private MockMvc mockMvc;
32+
@Before
33+
public void setUp() throws Exception {
34+
mockMvc = MockMvcBuilders.standaloneSetup( new CustomerController(customerRepository))
35+
.build();
36+
}
37+
38+
@Test
39+
public void whenInsertingCustomers_thenCustomersAreCreated() throws Exception {
40+
this.mockMvc.perform(post("/customers"))
41+
.andExpect(status().isOk());
42+
}
43+
44+
}

0 commit comments

Comments
 (0)