Skip to content

Commit c086a49

Browse files
Fetch association in batches
1 parent d294278 commit c086a49

File tree

1 file changed

+9
-4
lines changed
  • HibernateSpringBootLoadBatchAssociation

1 file changed

+9
-4
lines changed

HibernateSpringBootLoadBatchAssociation/README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
**[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.
42

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.
610

711
**Key points:**
812
- `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)`
1015

1116
-------------------------------
1217

0 commit comments

Comments
 (0)