You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: HibernateSpringBootLoadBatchAssociation/README.md
+9-4Lines changed: 9 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,17 @@
1
1
**[Fetching Associations In Batches Via `@BatchSize`](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootLoadBatchAssociation)**
2
-
3
-
**Note:** Fetching associations in the same query with their parent can be done via `JOIN FETCH` or `@NamedEntityGraph`. But, Hibernate allows us to fetch associations in batches as well via `@BatchSize` annotation. This may be useful when `JOIN FETCH` and `@NamedEntityGraph` doesn't seem to help.
4
2
5
-
**Description:** This application fetches all `Author` entities via a `SELECT` query. Further, calling the `getBooks()` method of the first `Author` entity will trigger another `SELECT` query that initializes the association of the first three `Author` entities returned by the previous `SELECT` query. This is the effect of `@BatchSize`.
3
+
**Description:** This application uses `@BatchSize` at class/entity-level and collection-level. Consider `Author` and `Book` entities invovled in a bidirectional-lazy `@OneToMany` association.
4
+
5
+
- First use case fetches all `Author` entities via a `SELECT` query. Further, calling the `getBooks()` method of the first `Author` entity will trigger another `SELECT` query that initializes the collections of the first three `Author` entities returned by the previous `SELECT` query. This is the effect of `@BatchSize` at `Author`'s collection-level.
6
+
7
+
- Second use case fetches all `Book` entities via a `SELECT` query. Further, calling the `getAuthor()` method of the first `Book` entity will trigger another `SELECT` query that initializes the authors of the first three `Book` entities returned by the previous `SELECT` query. This is the effect of `@BatchSize` at `Author` class-level.
8
+
9
+
**Note:** Fetching associated collections in the same query with their parent can be done via `JOIN FETCH` or entity graphs as well. Fetching children with their parents in the same query can be done via `JOIN FETCH`, entity graphs and `JOIN` as well.
6
10
7
11
**Key points:**
8
12
-`Author` and `Book` are in a lazy relationship (e.g., `@OneToMany` bidirectional relationship)
9
-
-`Author` association is annotated with `@BatchSize(size = 3)`
13
+
-`Author` entity is annotated with `@BatchSize(size = 3)`
14
+
-`Author`'s collection is annotated with `@BatchSize(size = 3)`
0 commit comments