[BAEL-2718] - Spring JPA Batch Inserts (#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)
This commit is contained in:
Blockchain-Trainer
2019-03-28 16:03:40 +05:30
committed by Josh Cummings
parent 728e5f39a9
commit 76ff0d25cf
6 changed files with 173 additions and 1 deletions
@@ -0,0 +1,43 @@
package com.baeldung.batchinserts;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.batchinserts.model.Customer;
import com.baeldung.batchinserts.repository.CustomerRepository;
/**
* A simple controller to test the JPA CrudRepository operations
* controllers
*
* @author ysharma2512
*
*/
@RestController
public class CustomerController {
@Autowired
CustomerRepository customerRepository;
public CustomerController(CustomerRepository customerRepository2) {
this.customerRepository = customerRepository2;
}
@PostMapping("/customers")
public ResponseEntity<List<Customer>> insertCustomers() throws URISyntaxException {
Customer c1 = new Customer("James", "Gosling");
Customer c2 = new Customer("Doug", "Lea");
Customer c3 = new Customer("Martin", "Fowler");
Customer c4 = new Customer("Brian", "Goetz");
List<Customer> customers = Arrays.asList(c1, c2, c3, c4);
customerRepository.saveAll(customers);
return ResponseEntity.ok(customers);
}
}
@@ -0,0 +1,56 @@
package com.baeldung.batchinserts.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* Customer Entity class
* @author ysharma2512
*
*/
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}
}
@@ -0,0 +1,15 @@
package com.baeldung.batchinserts.repository;
import org.springframework.data.repository.CrudRepository;
import com.baeldung.batchinserts.model.Customer;
/**
* JPA CrudRepository interface
*
* @author ysharma2512
*
*/
public interface CustomerRepository extends CrudRepository<Customer, Long>{
}
@@ -14,4 +14,9 @@ hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFa
spring.datasource.data=import_entities.sql
spring.main.allow-bean-definition-overriding=true
spring.main.allow-bean-definition-overriding=true
spring.jpa.properties.hibernate.jdbc.batch_size=4
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.generate_statistics=true