Skip to main content

Posts

Showing posts from June, 2022

Understanding Hibernate @BatchSize

Greetings! @BatchSize is one of those misunderstood concepts in Hibernate. I myself thought it was for the collection but I was wrong. Not convinced yet? let's find it out. (Simple example repo can be found here https://github.com/slmanju/hibernate-batchsize ) Let's assume we have a one-to-many relationship with Foo and Bar where Foo is the parent. Whether this is lazy or eager does not matter but I'll use Lazy loading. @Entity @Table(name = "foo") public class Foo { @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name="foo_id") private List<Bar> bars = new ArrayList<>(); } I will have 5 Foos each having 10 Bars. for (int i = 0; i < 5; i++) { Foo foo = Foo.getInstance("Foo-" + i); for (int j = 0; j < 10; j++) { foo.addBar(Bar.getInstance("Bar-" + i + "-" + j)); } entityManager.persist(foo); } Single Entity When we try to access the Bar collection from Foo what