0

I have a problem with hibernate which seems very slow when I try to persist my object in database.

So, the concerned model is like this :

@Entity
@Table(name = "T_PARENT")
public class ParentEntity implements Serializable {

    // Id

    // Attributes

    // Child relation
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name="xxxx")
    private List<ChildEntity> childs;

    // Getters & setters
}

@Entity
@Table(name = "T_CHILD")
public class ChildEntity implements Serializable {

    // Id

    // Attributes

    // Child relation
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name="yyyyy")
    private List<ChildChildEntity> childs;

    // Getters & setters
}

@Entity
@Table(name = "T_CHILD_CHILD")
public class ChildChildEntity implements Serializable {

    // Id

    // Attributes

    // Getters & setters
}

I use JpaRepository (spring) to manage data in database. We call "save" method on "parent" to persist the "child" and "childChild" (Only parent's repository was created)

With few data, there is no problem (ex : 1 parent / 20 child and for every child there 200 childChild) But when we have an important volume, the hibernate queries are interminables... (ex : 1 parent / 40 child and for every child there are 600 childChild)

Have you any idea how to improve performance with keeping the use of only one repository (parent) because I don't want to split the treatment...

Thanks

Mohamed
  • 2,342
  • 4
  • 21
  • 32
  • Please check my answer https://stackoverflow.com/a/50882952 - it should help.. – Cepr0 Jul 17 '18 at 14:47
  • I couldn't call saveAll method on a single object (parent object that I persist)... – Mohamed Jul 18 '18 at 07:28
  • For singe object (parent) you can use `save()` as well. It will save children with batching also. – Cepr0 Jul 18 '18 at 07:31
  • For 24000 inserts, it takes over 8 minutes... – Mohamed Jul 18 '18 at 07:33
  • Then use `saveAll` - see my example linked in the answer... First - prepare objects in the loop, then save them. – Cepr0 Jul 18 '18 at 07:34
  • As i said below, I want to persist parent and childs by calling parentRepo.save(parentObj), I'm looking for a solution without splitting the repositories (if there is a solution)... – Mohamed Jul 18 '18 at 07:38

1 Answers1

1

You could try to use batch inserts. That way Hibernate will send batches of insert statements to the database and not every single insert.

Set hibernate.jdbc.batch_size to a value up to 30. But be careful and test if it boosts your performance.

Read more about that: https://vladmihalcea.com/how-to-batch-insert-and-update-statements-with-hibernate/

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • I have already added that property, but nothing changes (properties.put("hibernate.jdbc.batch_size", 100);) – Mohamed Jul 17 '18 at 13:45
  • the question is if Hibernate is the right tool for mass import. Have you ever thought about using plain SQL? Or something like jOOQ? – Simon Martinelli Jul 17 '18 at 14:24
  • It is a little bit late for me to change the manner that I used to save data, for that I try to improve the performance. In addition to that, I think that 20000 inserts should not take 5 minutes to be executed... – Mohamed Jul 17 '18 at 14:27