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
This chapter covers Spring Data's Ahead of Time (AOT) optimizations that build upon {spring-framework-docs}/core/aot.html[Spring's Ahead of Time Optimizations].
3
+
[[aot.repositories.jpa]]
4
+
== JPA Ahead of Time Repositories
4
5
5
-
[[aot.bestpractices]]
6
-
== Best Practices
7
-
8
-
=== Annotate your Domain Types
9
-
10
-
During application startup, Spring scans the classpath for domain classes for early processing of entities.
11
-
By annotating your domain types with Spring Data-specific `@Table`, `@Document` or `@Entity` annotations you can aid initial entity scanning and ensure that those types are registered with `ManagedTypes` for Runtime Hints.
12
-
Classpath scanning is not possible in native image arrangements and so Spring has to use `ManagedTypes` for the initial entity set.
13
-
14
-
[[aot.hints]]
15
-
== Runtime Hints
16
-
17
-
Running an application as a native image requires additional information compared to a regular JVM runtime.
18
-
Spring Data contributes {spring-framework-docs}/core/aot.html#aot.hints[Runtime Hints] during AOT processing for native image usage.
19
-
These are in particular hints for:
20
-
21
-
* Auditing
22
-
* `ManagedTypes` to capture the outcome of class-path scans
23
-
* Repositories
24
-
** Reflection hints for entities, return types, and Spring Data annotations
25
-
** Repository fragments
26
-
** Querydsl `Q` classes
27
-
** Kotlin Coroutine support
28
-
* Web support (Jackson Hints for `PagedModel`)
29
-
30
-
[[aot.repositories]]
31
-
== Ahead of Time Repositories
32
-
33
-
AOT Repositories are an extension to AOT processing by pre-generating eligible query method implementations.
34
-
Query methods are opaque to developers regarding their underlying queries being executed in a query method call.
35
-
AOT repositories contribute query method implementations based on derived, annotated, and named queries that are known at build-time.
36
-
This optimization moves query method processing from runtime to build-time, which can lead to a significant performance improvement as query methods do not need to be analyzed reflectively upon each application start.
37
-
38
-
The resulting AOT repository fragment follows the naming scheme of `<Repository FQCN>Impl__Aot` and is placed in the same package as the repository interface.
39
-
You can find all queries in their String form for generated repository query methods.
40
-
41
-
NOTE: Consider AOT repository classes an internal optimization.
42
-
Do not use them directly in your code as generation and implementation details may change in future releases.
43
-
44
-
=== Running with AOT Repositories
45
-
46
-
AOT is a mandatory step to transform a Spring application to a native executable, so it is automatically enabled when running in this mode.
47
-
When AOT is enabled (either for native compilation or by setting `spring.aot.enabled=true`), AOT repositories are automatically enabled by default.
48
-
49
-
You can disable AOT repository generation entirely or only disable JPA AOT repositories:
50
-
51
-
* Set the `spring.aot.repositories.enabled=false` property to disable generated repositories for all Spring Data modules.
52
-
* Set the `spring.aot.jpa.repositories.enabled=false` property to disable only JPA AOT repositories.
53
-
54
-
AOT repositories contribute configuration changes to the actual repository bean registration to register the generated repository fragment.
55
-
56
-
NOTE: When AOT optimizations are included, some decisions that have been taken at build-time are hard-coded in the application setup.
57
-
For instance, profiles that have been enabled at build-time are automatically enabled at runtime as well.
58
-
Also, the Spring Data module implementing a repository is fixed.
59
-
Changing the implementation requires AOT re-processing.
6
+
AOT repositories filter methods that are eligible for AOT processing.
7
+
These are typically all query methods that are not backed by an xref:repositories/custom-implementations.adoc[implementation fragment].
60
8
61
-
NOTE: AOT processing avoids database access.
9
+
[NOTE]
10
+
====
11
+
AOT processing avoids database access.
62
12
Therefore, it initializes an in-memory Hibernate instance for metadata collection.
63
13
Types for the Hibernate configuration are determined by our AOT metadata collector.
64
14
We prefer using a `PersistentEntityTypes` bean if available and fall back to `PersistenceUnitInfo` or our own discovered types.
65
15
If our type scanning is not sufficient for your arrangement, you can enable direct `EntityManagerFactory` usage by configuring the `spring.aot.jpa.repositories.use-entitymanager=true` property.
66
-
67
-
=== Eligible Methods
68
-
69
-
AOT repositories filter methods that are eligible for AOT processing.
70
-
These are typically all query methods that are not backed by an xref:repositories/custom-implementations.adoc[implementation fragment].
16
+
====
71
17
72
18
**Supported Features**
73
19
@@ -85,127 +31,12 @@ Mind that using Value Expressions requires expression parsing and contextual inf
85
31
86
32
* Requires Hibernate for AOT processing.
87
33
* `QueryRewriter` must be a no-args class. `QueryRewriter` beans are not yet supported.
88
-
* Methods accepting `ScrollPosition` (e.g. `Keyset` pagination) are not yet supported
34
+
* Methods accepting `ScrollPosition` (e.g. `Keyset` pagination) are not yet supported.
35
+
* Custom Collection return types (e.g. `io.vavr.collection`, `"org.eclipse.collections`) are not yet supported.
89
36
90
37
**Excluded methods**
91
38
92
39
* `CrudRepository`, Querydsl, Query by Example, and other base interface methods as their implementation is provided by the base class respective fragments
93
40
* Methods whose implementation would be overly complex
* `query`: Query descriptor if the method is a query method.
190
-
** `name`: Name of the named query if the query is a named one.
191
-
** `query` the query used to obtain the query method result from `EntityManager`
192
-
** `count-name`: Name of the named count query if the count query is a named one.
193
-
** `count-query`: The count query used to obtain the count for query methods using pagination.
194
-
** `procedure-name`: Name of the named stored procedure if the stored procedure is a named one.
195
-
** `procedure`: Stored procedure name if the query method uses stored procedures.
196
-
* `fragment`: Target fragment if the method call is delegated to a store (repository base class, functional fragment such as Querydsl) or user fragment.
197
-
Fragments are either described with just `fragment` if there is no further interface or as `interface` and `fragment` tuple in case there is an interface (such as Querydsl or user-declared fragment interface).
198
-
199
-
[NOTE]
200
-
.Normalized Query Form
201
-
====
202
-
Static analysis of queries allows only a limited representation of runtime query behavior.
203
-
Queries are represented in their normalized (pre-parsed and rewritten) form:
204
-
205
-
* Value Expressions are replaced with bind markers.
206
-
* Queries follow the specified query language (JPQL or native) and do not represent the final SQL query.
207
-
Spring Data cannot derive the final SQL queries as this is database-specific and depends on the actual runtime environment and parameters (e.g. Entity Graphs, Lazy Loading).
208
-
* Query Metadata does not reflect bind-value processing.
209
-
`StartingWith`/`EndingWith` queries prepend/append the wildcard character `%` to the actual bind value.
210
-
* Runtime Sort information cannot be incorporated in the query string itself as that detail is not known at build-time.
0 commit comments