【发布时间】:2021-03-07 08:13:14
【问题描述】:
我正在尝试使用 JpaRepository 实现批量插入,但即使我使用推荐的属性,它似乎也不起作用。这是我的代码:
实体 - Book.java:
@Entity(name = "books")
@Table(name = "books")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String title;
private String author;
private String edition;
private String status;
@Column(unique = true)
private String isbn;
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL,mappedBy = "book", fetch = FetchType.LAZY)
private List<Image> images = new ArrayList<>();
// Getters and Setters omitted
}
服务 - BookServiceImpl
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookRepository bookRepository;
@Override
public List<Book> storeBooks(List<Book> books) {
return bookRepository.saveAll(books);
}
}
属性 - application.properties:
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost/bookdb?reWriteBatchedInserts=true
spring.datasource.username=**
spring.datasource.password=**
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.jdbc.batch_size=100
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.generate_statistics=true
插入后的SQL日志:
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into books (author, edition, isbn, status, title, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into books (author, edition, isbn, status, title, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into books (author, edition, isbn, status, title, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into books (author, edition, isbn, status, title, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into books (author, edition, isbn, status, title, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into books (author, edition, isbn, status, title, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into books (author, edition, isbn, status, title, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into books (author, edition, isbn, status, title, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into books (author, edition, isbn, status, title, id) values (?, ?, ?, ?, ?, ?)
2021-03-07 09:57:50.163 INFO 7800 --- [nio-8080-exec-1] i.StatisticalLoggingSessionEventListener : Session Metrics {
2883700 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
9612998 nanoseconds spent preparing 10 JDBC statements;
23803401 nanoseconds spent executing 9 JDBC statements;
23764601 nanoseconds spent executing 1 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
275826200 nanoseconds spent executing 1 flushes (flushing a total of 9 entities and 9 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
我不知道问题是出在日志还是其他方面,但我按照建议实施了所有内容...
【问题讨论】:
-
您是否在某处定义了事务?可能插入尚未提交。
-
问题出在记录器上,默认的休眠记录器,因为默认情况下它不显示 SQL 插入是否被批处理
-
23764601 nanoseconds spent executing 1 JDBC batches;--> 这表明你是分批执行的。baeldung.com/spring-data-jpa-batch-inserts#console 参考这个链接。 -
@RaghuDinkaVijaykumar 是的,我现在意识到批量插入已经在工作,问题是 Hibernate 默认日志不会显示 SQL 插入是否被批量处理,所以解决方案是实现 BeanPostProcessor并添加两个依赖,SLF4J 和数据源代理
标签: spring spring-boot jpa