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
@@ -227,26 +228,21 @@ Besides that, Spring Data supports to return other wrapper types on query method
227
228
228
229
Alternatively query methods can choose not to use a wrapper type at all.
229
230
The absence of a query result will then be indicated by returning `null`.
230
-
Repository methods returning collections, wrappers, and streams are guaranteed never to return `null` but rather the corresponding empty representation.
231
+
Repository methods returning collections, collection alternatives, wrappers, and streams are guaranteed never to return `null` but rather the corresponding empty representation.
232
+
See <<repository-query-return-types>> for details.
231
233
232
234
[[repositories.nullability.annotations]]
233
235
==== Nullability annotations
234
236
235
-
You can express null-safe repository methods by using link:{spring-framework-docs}core.html#null-safety[Spring Framework's annotations].
236
-
They provide a tooling-friendly approach and opt-in for `null` checks during runtime:
237
+
You can express nullability constraints for repository methods using link:{spring-framework-docs}/core.html#null-safety[Spring Framework's nullability annotations].
238
+
They provide a tooling-friendly approach and opt-in `null` checks during runtime:
annotation where a specific parameter or return value cannot be `null`
240
+
* {spring-framework-javadoc}/org/springframework/lang/NonNullApi.html[`@NonNullApi`] – to be used on the package level to declare that the default behavior for parameters and return values is to not accept or produce `null` values.
241
+
* {spring-framework-javadoc}/org/springframework/lang/NonNull.html[`@NonNull`] – to be used on a parameter or return value that must not be `null`
240
242
(not needed on parameter and return value where `@NonNullApi` applies).
annotation at package level declares non-null as the default behavior for parameters and return values.
243
+
* {spring-framework-javadoc}/org/springframework/lang/Nullable.html[`@Nullable`] – to be used on a parameter or return value that can be `null`.
247
244
248
245
Spring annotations are meta-annotated with https://jcp.org/en/jsr/detail?id=305[JSR 305] annotations (a dormant but widely spread JSR). JSR 305 meta-annotations allow tooling vendors like https://www.jetbrains.com/help/idea/nullable-and-notnull-annotations.html[IDEA], http://help.eclipse.org/oxygen/index.jsp?topic=/org.eclipse.jdt.doc.user/tasks/task-using_external_null_annotations.htm[Eclipse], or link:https://kotlinlang.org/docs/reference/java-interop.html#null-safety-and-platform-types[Kotlin] to provide null-safety support in a generic way, without having to hard-code support for Spring annotations.
249
-
250
246
To enable runtime checking of nullability constraints for query methods, you need to activate non-nullability on package level using Spring’s `@NonNullApi` in `package-info.java`:
251
247
252
248
.Declaring non-nullability in `package-info.java`
@@ -260,39 +256,39 @@ package com.acme;
260
256
261
257
Once non-null defaulting is in place, repository query method invocations will get validated at runtime for nullability constraints.
262
258
Exceptions will be thrown in case a query execution result violates the defined constraint, i.e. the method would return `null` for some reason but is declared as non-nullable (the default with the annotation defined on the package the repository resides in).
263
-
If you want to opt-in to nullable results again, use selectively `@Nullable` on a method.
264
-
259
+
If you want to opt-in to nullable results again, selectively use `@Nullable` that a method.
265
260
Using the aforementioned result wrapper types will continue to work as expected, i.e. an empty result will be translated into the value representing absence.
<1> Will throw an `EmptyResultDataAccessException` in case the query executed does not produce a result. Will throw an `IllegalArgumentException` in case the `emailAddress` handed to the method is `null`.
286
-
<2> Will return `null` in case the query executed does not produce a result. Also accepts `null` as value for `emailAddress`.
287
-
<3> Will return `Optional.empty()` in case the query executed does not produce a result. Will throw an `IllegalArgumentException` in case the `emailAddress` handed to the method is `null`.
280
+
<1> The repository resides in a package (or sub-package) for which we've defined non-null behavior (see above).
281
+
<2> Will throw an `EmptyResultDataAccessException` in case the query executed does not produce a result. Will throw an `IllegalArgumentException` in case the `emailAddress` handed to the method is `null`.
282
+
<3> Will return `null` in case the query executed does not produce a result. Also accepts `null` as value for `emailAddress`.
283
+
<4> Will return `Optional.empty()` in case the query executed does not produce a result. Will throw an `IllegalArgumentException` in case the `emailAddress` handed to the method is `null`.
288
284
====
289
285
290
286
[[repositories.nullability.kotlin]]
291
287
==== Nullability in Kotlin-based repositories
292
288
293
289
Kotlin has the definition of https://kotlinlang.org/docs/reference/null-safety.html[nullability constraints]
294
290
baked into the language.
295
-
Kotlin code compiles to bytecode which does not express nullability constraints using method signatures but rather compiled-in metadata. Make sure to include `kotlin-reflect` to enable introspection of Kotlin's nullability constraints.
291
+
Kotlin code compiles to bytecode which does not express nullability constraints using method signatures but rather compiled-in metadata. Make sure to include the `kotlin-reflect` JAR in your project to enable introspection of Kotlin's nullability constraints.
296
292
Spring Data repositories use the language mechanism to define those constraints to apply the same runtime checks:
297
293
298
294
.Using nullability constraints on Kotlin repositories
@@ -301,9 +297,9 @@ Spring Data repositories use the language mechanism to define those constraints
fun findByFirstname(firstname: String?): User? <2>
302
+
fun findByFirstname(firstname: String?): User? <2>
307
303
}
308
304
----
309
305
<1> The method defines both, the parameter as non-nullable (the Kotlin default) as well as the result. The Kotlin compiler will already reject method invocations trying to hand `null` into the method. In case the query execution yields an empty result, an `EmptyResultDataAccessException` will be thrown.
@@ -582,7 +578,7 @@ NOTE: Not all Spring Data modules currently support `Stream<T>` as a return type
582
578
[[repositories.query-async]]
583
579
=== Async query results
584
580
585
-
Repository queries can be executed asynchronously using link:{spring-framework-docs}integration.html#scheduling[Spring's asynchronous method execution capability]. This means the method will return immediately upon invocation and the actual query execution will occur in a task that has been submitted to a Spring TaskExecutor.
581
+
Repository queries can be executed asynchronously using link:{spring-framework-docs}/integration.html#scheduling[Spring's asynchronous method execution capability]. This means the method will return immediately upon invocation and the actual query execution will occur in a task that has been submitted to a Spring TaskExecutor.
0 commit comments