Skip to content

Commit e489df5

Browse files
Update README.md
1 parent fbdaeb1 commit e489df5

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,14 +2124,19 @@ The trick is to simply define a method named `fetchAll()` that uses JPQL and `P
21242124
----------------------------------------------------------------------------------------------------------------------
21252125

21262126
141. **[Fetching Associations In Batches Via `@BatchSize`](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootLoadBatchAssociation)**
2127-
2128-
**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.
21292127

2130-
**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`.
2128+
**Description:** This application uses Hibernate specific `@BatchSize` at class/entity-level and collection-level. Consider `Author` and `Book` entities invovled in a bidirectional-lazy `@OneToMany` association.
2129+
2130+
- 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.
2131+
2132+
- 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.
2133+
2134+
**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.
21312135

21322136
**Key points:**
21332137
- `Author` and `Book` are in a lazy relationship (e.g., `@OneToMany` bidirectional relationship)
2134-
- `Author` association is annotated with `@BatchSize(size = 3)`
2138+
- `Author` entity is annotated with `@BatchSize(size = 3)`
2139+
- `Author`'s collection is annotated with `@BatchSize(size = 3)`
21352140

21362141
----------------------------------------------------------------------------------------------------------------------
21372142

0 commit comments

Comments
 (0)