Skip to content

Commit 7635632

Browse files
Added docs for FetchType Lazy
1 parent abb8360 commit 7635632

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

docs/modules/ROOT/pages/features/lazies.adoc

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,46 @@ import software.xdev.spring.data.eclipse.store.repository.lazy.SpringDataEclipse
2525
2626
public class Owner extends Person
2727
{
28-
private String address;
29-
28+
//...
3029
private final Lazy<List<Pet>> pets = SpringDataEclipseStoreLazy.build(new ArrayList<>());
3130
//...
3231
----
3332

33+
== FetchType.LAZY
34+
35+
In Spring JPA, lazy loading is achieved by annotating a field or property with ``FetchType.LAZY``.
36+
This approach leverages JPA's built-in mechanisms to defer the retrieval of the related entity until it is accessed.
37+
38+
In contrast, {product-name} takes a different approach.
39+
Instead of using annotations, you wrap the object intended to be loaded lazily in a ``Lazy``-wrapper.
40+
This wrapper encapsulates the object and ensures it is only loaded when needed.
41+
42+
[source,java,title="JPA Example with lazy"]
43+
----
44+
import jakarta.persistence.OneToMany;
45+
46+
public class Owner extends Person
47+
{
48+
@OneToMany(fetch = FetchType.LAZY)
49+
private final List<Pet> pets = new ArrayList<>();
50+
//...
51+
----
52+
53+
[source,java,title="https://github.com/xdev-software/spring-data-eclipse-store/tree/develop/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/complex/owner/Owner.java[Slightly changed example from complex demo]"]
54+
----
55+
package software.xdev.spring.data.eclipse.store.demo.complex.owner;
56+
//...
57+
import software.xdev.spring.data.eclipse.store.repository.lazy.SpringDataEclipseStoreLazy;
58+
59+
public class Owner extends Person
60+
{
61+
private final List<Lazy<Pet>> pets = new ArrayList<>();
62+
//...
63+
----
64+
65+
The ``Lazy``-wrapper makes lazy loading **explicit and flexible**, avoiding JPA-specific overhead and potential exceptions.
66+
But it introduces a custom, less-standardized approach that may increase boilerplate and requires developers to remember to use the wrapper, which could lead to errors if overlooked.
67+
3468
== Repositories
3569

3670
Entities in a repository are by default **not lazy**.

0 commit comments

Comments
 (0)