Home » Java » java – Performance Hibernate/JPA/Spring insert nested child-Exceptionshub

java – Performance Hibernate/JPA/Spring insert nested child-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have two entities as follow:

class A {

   @Id
   @GeneratedValue(generator = "genA", strategy = GenerationType.SEQUENCE)
   @SequenceGenerator(name = "genA", sequenceName = "SQ_A",    allocationSize = ALLOCATION_SIZE)
   Long id;

   @OneToOne
   @JoinColumn(name = "B_ID")
   B b;

}

class B {
   @Id
   @GeneratedValue(generator = "genB", strategy = GenerationType.SEQUENCE)
   @SequenceGenerator(name = "genB", sequenceName = "SQ_B",    allocationSize = ALLOCATION_SIZE)
   Long id;
}

I use allocationSize, batch_size, batch_insert and batch_update in order to increase performance for insertion. It works well with collections @OneToMany but I struggle with @OneToOne as the previous code shows. I use JpaRepository.save / saveAll method. I have the same issue if flush/clear manually.

Hibernate always do

insert tableB
insert tableA
insert tableB
insert tableA
insert tableB
insert tableA

which is very slow compared to other batch_insert I have.

What’s I’m trying to understand is why Hibernate doesn’t do something like that:

--- batch_insert B ---
insert tableB
insert tableB
insert tableB
--- batch_insert A ---
insert tableA
insert tableA
insert tableA
--- batch_update A ---
update tableA
update tableA
update tableA

Thanks for your help! 🙂

PS: My yml properties:

hibernate.jdbc.batch_size: 200
hibernate.jdbc.batch_versioned_data: true;
hibernate.jdbc.order_inserts: true
hibernate.jdbc.order_updates: true
How to&Answers:

I used the wrong yml settings for the order_insert and order_update properties:

    hibernate.jdbc.order_inserts: true
    hibernate.jdbc.order_updates: true

instead of

    hibernate.order_inserts: true
    hibernate.order_updates: true