flobuc
flobuc

Reputation: 31

Hibernate doesn't use batch-processing at bidirectional OneToMany-relation with JoinTable

I have several entities with a bidirectional @OneToMany-relation with @JoinTable. Functionality is working fine, besides the performance of inserting large amounts of datasets to the database. I found out that in this case Hibernate isn't using batch-processing for all entities. Hibernate using batch-processing for the parent-entities, but not for the childs. It inserts all parent-datasets to the database with batch-processing, but then it generates a child-dataset, then the associated mappingtable-dataset, then the next child-dataset, then the associated mappingtable-dataset and so on, all with single statements.

When i change the @OneToMany-Relation for test to a @ManyToMany-Relation then batch-processing is used for all datasets, first all parents, then all childs, then all mappings and the performance is increasing dramatically. Also on a unidirectional OneToMany-Relation batch-processing is working fine.

Can somebody explain me, how to configure a bidirectional @OneToMany-relation with @JoinTable so that Hibernate uses batch-processing also for childs?

Easy example to reproduce: Parent A:

package com.flobuc;

import javax.persistence.*;
import java.util.Collection;

@Entity
@Table(name = "A")
public class A {
    private String id;
    private String name;
    private Collection<B> bs;

    @Id
    @Column(name = "ID")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Basic
    @Column(name = "NAME")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    @OneToMany(cascade = CascadeType.ALL, mappedBy = "a")
    public Collection<B> getBs() {
        return bs;
    }

    public void setBs(Collection<B> bs) {
        this.bs = bs;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        A a = (A) o;

        if (id != null ? !id.equals(a.id) : a.id != null) return false;
        return name != null ? name.equals(a.name) : a.name == null;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }
}

Child B:

package com.flobuc;

import javax.persistence.*;

@Entity
@Table(name = "B")
public class B {
    private String id;
    private String name;
    private A a;

    @Id
    @Column(name = "ID")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Basic
    @Column(name = "NAME")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @ManyToOne
    @JoinTable(
            name="A2B",
            joinColumns = {@JoinColumn(name = "B_ID", referencedColumnName = "ID", nullable = false)},
            inverseJoinColumns={@JoinColumn(name = "A_ID", referencedColumnName = "ID", nullable = false)})
    public A getA() {
        return a;
    }

    public void setA(A a) {
        this.a = a;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        B b = (B) o;

        if (id != null ? !id.equals(b.id) : b.id != null) return false;
        return name != null ? name.equals(b.name) : b.name == null;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }
}

Generate Entities and persist:

List<A> as = new ArrayList<>();

for (int i = 0; i < 100 ; i++) {

    A a = new A();
    a.setId("ID-A-" + i);
    a.setName("Name ID-" + i);

    a.setBs(new HashSet<>());

    for (int j = 0; j < 50 ; j++) {
        B b =new B();
        b.setId("ID-B-" + j + "ID-A-" + i);
        b.setName("Name ID-" + j + " Parent " + i);
        b.setA(a);
        a.getBs().add(b);
    }

    as.add(a);

}

for(A a : as){
    em.persist(a);
}

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
                                 http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

    <persistence-unit name="TimetablePU" transaction-type="JTA">

        <jta-data-source>java:/TimetableDS</jta-data-source>
        <class>com.atron.atries.rx.asdm.dao.entity.netex.A</class>
        <class>com.atron.atries.rx.asdm.dao.entity.netex.B</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
            <property name="hibernate.jdbc.batch_size" value="10"/>
            <property name="hibernate.order_inserts" value="true"/>
            <property name="hibernate.order_updates" value="true"/>
            <property name="hibernate.jdbc.batch_versioned_data" value="true"/>                
        </properties>

    </persistence-unit>

</persistence>

Env: WildFly 10.1, Hibernate 5.0.1

Upvotes: 2

Views: 168

Answers (0)

Related Questions