I followed this example (How to do bulk (multi row) inserts with JpaRepository?) and created an h2-database example case. But bulk-insert doesn't work.
Model:
@Table(name = "user")
@Entity
public class User
{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String name;
}
Repository:
public interface IUserRepository extends JpaRepository<User, Long>
{
}
Test:
@SpringBootApplication
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class, args);
}
@Bean
ApplicationRunner init(IUserRepository userRepository)
{
return args -> {
User user = new User();
user.setName("Test-1");
User user1 = new User();
user1.setName("Test-2");
userRepository.saveAll(Arrays.asList(user, user1));
};
}
}
properties:
spring.datasource.url=jdbc:h2:~/test;TRACE_LEVEL_FIle=4
spring.jpa.hibernate.ddl-auto=create
spring.datasource.username=sa
spring.jpa.properties.hibernate.jdbc.batch_size=5
Output:
/*SQL #:1*/call next value for hibernate_sequence;
2021-02-28 15:13:35.110 INFO 43465 --- [ main] h2database
/*SQL l:58 #:1*/SELECT VALUE FROM INFORMATION_SCHEMA.SETTINGS WHERE NAME=? {1: 'QUERY_TIMEOUT'};
2021-02-28 15:13:35.119 INFO 43465 --- [ main] h2database
/*SQL #:1*/call next value for hibernate_sequence;
2021-02-28 15:13:35.136 INFO 43465 --- [ main] h2database
/*SQL l:41 #:1*/insert into user (name, id) values (?, ?) {1: 'Test-1', 2: 1};
2021-02-28 15:13:35.137 INFO 43465 --- [ main] h2database
/*SQL l:41 #:1*/insert into user (name, id) values (?, ?) {1: 'Test-2', 2: 2};
2021-02-28 15:13:35.139 INFO 43465 --- [ main] h2database
/*SQL */COMMIT;
2021-02-28 15:13:35.139 INFO 43465 --- [ main] h2database
/*SQL */COMMIT;
How can I test bulk-insert in h2-database with using Spring-Boot? Or is it possible?