'Hibernate inheritance issues when batch inserting

I have a class that extends another one (both entities). The parent and child share the same primary key

I'm trying to insert in batch 20k rows (of child entity), using saveAll method of spring-jpa repository, but checking the log you can see it's executing 40k batches. I was expecting that it would insert ALL the parents then ALL the children as an optimization or somehow.

There is no batch at all.

parent

@Entity
@Table(name = "orders")
@Inheritance(strategy = InheritanceType.JOINED)
public class Order implements Serializable {

@Id
@SequenceGenerator(name = "sq_order_gen", allocationSize = 1, sequenceName = "sq_order")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sq_order_gen")
@Column(name = "order_id")
private Long id;

child

@Entity
@Table(name = "orders_infos")
public class OrderInfos extends Order implements Serializable {

Logs of inserting

2021-12-22 09:44:57.485  INFO [testBE,deabba7e41a880a1,deabba7e41a880a1,true] 37668 --- [nio-8198-exec-6] i.StatisticalLoggingSessionEventListener : Session Metrics {
    3149500 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    1255278700 nanoseconds spent preparing 40002 JDBC statements;
    91509100 nanoseconds spent executing 2 JDBC statements;
    163476681800 nanoseconds spent executing 40000 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    223333492400 nanoseconds spent executing 1 flushes (flushing a total of 20000 entities and 0 collections);
    0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)

Is there any workaround?

env: Springboot 2.3.9 with spring jpa -> Hibernate 5.4.29

spring.jpa.properties.hibernate.jdbc.batch_size=30
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.generate_statistics=true

I see a similarity in this post https://stchedroff.wordpress.com/2012/12/20/jpa-hibernate-object-inheritance-batch-insert/



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source