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
private final Lazy<List<Pet>> pets = SpringDataEclipseStoreLazy.build(new ArrayList<>());
31
30
//...
32
31
----
33
32
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]"]
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
+
34
68
== Repositories
35
69
36
70
Entities in a repository are by default **not lazy**.
0 commit comments