Skip to content

Commit eb10650

Browse files
Add files via upload
1 parent 53cbfe4 commit eb10650

File tree

8 files changed

+322
-0
lines changed

8 files changed

+322
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<actions>
3+
<action>
4+
<actionName>run</actionName>
5+
<packagings>
6+
<packaging>jar</packaging>
7+
</packagings>
8+
<goals>
9+
<goal>spring-boot:run</goal>
10+
</goals>
11+
<properties>
12+
<spring-boot.run.jvmArguments>-noverify -XX:TieredStopAtLevel=1 </spring-boot.run.jvmArguments>
13+
<spring-boot.run.mainClass>com.jpa.JpaApplication</spring-boot.run.mainClass>
14+
<Env.SPRING_OUTPUT_ANSI_ENABLED>always</Env.SPRING_OUTPUT_ANSI_ENABLED>
15+
</properties>
16+
</action>
17+
<action>
18+
<actionName>debug</actionName>
19+
<packagings>
20+
<packaging>jar</packaging>
21+
</packagings>
22+
<goals>
23+
<goal>spring-boot:run</goal>
24+
</goals>
25+
<properties>
26+
<spring-boot.run.jvmArguments>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -noverify -XX:TieredStopAtLevel=1 </spring-boot.run.jvmArguments>
27+
<spring-boot.run.mainClass>com.jpa.JpaApplication</spring-boot.run.mainClass>
28+
<Env.SPRING_OUTPUT_ANSI_ENABLED>always</Env.SPRING_OUTPUT_ANSI_ENABLED>
29+
<jpda.listen>true</jpda.listen>
30+
</properties>
31+
</action>
32+
<action>
33+
<actionName>profile</actionName>
34+
<packagings>
35+
<packaging>jar</packaging>
36+
</packagings>
37+
<goals>
38+
<goal>process-classes</goal>
39+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
40+
</goals>
41+
<properties>
42+
<exec.args>-classpath %classpath com.jpa.JpaApplication</exec.args>
43+
<exec.executable>java</exec.executable>
44+
</properties>
45+
</action>
46+
</actions>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.jpa</groupId>
7+
<artifactId>jpa</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>jpa</name>
12+
<description>JPA project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.5.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-jdbc</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>mysql</groupId>
42+
<artifactId>mysql-connector-java</artifactId>
43+
<scope>runtime</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-test</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
</dependencies>
51+
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-maven-plugin</artifactId>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
61+
62+
</project>
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.jpa;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.ApplicationRunner;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.context.annotation.Bean;
10+
11+
@SpringBootApplication
12+
public class BatchInsertsApplication {
13+
14+
@Autowired
15+
private Dao dao;
16+
17+
public static void main(String[] args) {
18+
SpringApplication.run(BatchInsertsApplication.class, args);
19+
}
20+
21+
@Bean
22+
public ApplicationRunner init() {
23+
return args -> {
24+
25+
User user_1 = new User();
26+
user_1.setId(1L);
27+
user_1.setName("Jacky Francisco");
28+
user_1.setCity("Banesti");
29+
user_1.setAge(24);
30+
31+
User user_2 = new User();
32+
user_2.setId(2L);
33+
user_2.setName("Caludiu George");
34+
user_2.setCity("Brasov");
35+
user_2.setAge(31);
36+
37+
User user_3 = new User();
38+
user_3.setId(3L);
39+
user_3.setName("Marius Botocoala");
40+
user_3.setCity("Sibiu");
41+
user_3.setAge(22);
42+
43+
User user_4 = new User();
44+
user_4.setId(4L);
45+
user_4.setName("Serban D");
46+
user_4.setCity("Constanta");
47+
user_4.setAge(52);
48+
49+
User user_5 = new User();
50+
user_5.setId(5L);
51+
user_5.setName("Marina Acoolea");
52+
user_5.setCity("Brasov");
53+
user_5.setAge(22);
54+
55+
User user_6 = new User();
56+
user_6.setId(6L);
57+
user_6.setName("Tayler Durden");
58+
user_6.setCity("Dallas");
59+
user_6.setAge(20);
60+
61+
User user_7 = new User();
62+
user_7.setId(7L);
63+
user_7.setName("Andy T");
64+
user_7.setCity("Mures");
65+
user_7.setAge(54);
66+
67+
List<User> users =
68+
Arrays.asList(user_1, user_2, user_3, user_4, user_5, user_6, user_7);
69+
70+
dao.saveInBatch(users);
71+
};
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.jpa;
2+
3+
import java.io.Serializable;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.logging.Level;
7+
import java.util.logging.Logger;
8+
import javax.persistence.EntityManager;
9+
import javax.persistence.PersistenceContext;
10+
import org.hibernate.Session;
11+
import org.springframework.stereotype.Repository;
12+
import org.springframework.transaction.annotation.Transactional;
13+
14+
@Repository
15+
@Transactional
16+
public class Dao<T, ID extends Serializable> implements GenericDao<T, ID> {
17+
18+
private static final Logger logger = Logger.getLogger(Dao.class.getName());
19+
20+
private static final int BATCH_SIZE = 3;
21+
22+
@PersistenceContext
23+
private EntityManager entityManager;
24+
25+
@Override
26+
public <S extends T> S persist(S entity) {
27+
entityManager.persist(entity);
28+
29+
return entity;
30+
}
31+
32+
@Override
33+
public <S extends T> Iterable<S> saveInBatch(Iterable<S> entities) {
34+
35+
if(entities == null) {
36+
throw new IllegalArgumentException("The given Iterable of entities not be null!");
37+
}
38+
39+
int i = 0;
40+
41+
Session session = entityManager.unwrap(Session.class);
42+
session.setJdbcBatchSize(BATCH_SIZE);
43+
44+
List<S> result = new ArrayList<>();
45+
46+
for (S entity : entities) {
47+
result.add(persist(entity));
48+
49+
i++;
50+
51+
// Flush a batch of inserts and release memory
52+
if (i % session.getJdbcBatchSize() == 0 && i > 0) {
53+
logger.log(Level.INFO,
54+
"Flushing the EntityManager containing {0} entities ...", i);
55+
56+
entityManager.flush();
57+
entityManager.clear();
58+
i = 0;
59+
}
60+
}
61+
62+
return result;
63+
}
64+
65+
protected EntityManager getEntityManager() {
66+
return entityManager;
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.jpa;
2+
3+
import java.io.Serializable;
4+
5+
public interface GenericDao<T, ID extends Serializable> {
6+
7+
<S extends T> S persist(S entity);
8+
9+
<S extends T> Iterable<S> saveInBatch(Iterable<S> entites);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.jpa;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Entity;
5+
import javax.persistence.Id;
6+
import javax.persistence.Version;
7+
8+
@Entity
9+
public class User implements Serializable {
10+
11+
private static final long serialVersionUID = 1L;
12+
13+
@Id
14+
private Long id;
15+
16+
private String name;
17+
private String city;
18+
private int age;
19+
20+
public Long getId() {
21+
return id;
22+
}
23+
24+
public void setId(Long id) {
25+
this.id = id;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
36+
public String getCity() {
37+
return city;
38+
}
39+
40+
public void setCity(String city) {
41+
this.city = city;
42+
}
43+
44+
public int getAge() {
45+
return age;
46+
}
47+
48+
public void setAge(int age) {
49+
this.age = age;
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
spring.datasource.url=jdbc:mysql://localhost:3306/db_users?rewriteBatchedStatements=true
2+
spring.datasource.username=root
3+
spring.datasource.password=root
4+
5+
spring.jpa.hibernate.ddl-auto=create
6+
spring.jpa.show-sql=true
7+
8+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
9+
spring.jpa.properties.hibernate.format-sql=true
10+
11+
spring.jpa.properties.hibernate.generate_statistics=true

0 commit comments

Comments
 (0)