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
As of Spring Data 2.0, repository CRUD methods that return an individual aggregate instance use Java 8's `Optional` to indicate the potential absence of a value.
5
+
Besides that, Spring Data supports returning the following wrapper types on query methods:
6
+
7
+
* `com.google.common.base.Optional`
8
+
* `scala.Option`
9
+
* `io.vavr.control.Option`
10
+
11
+
Alternatively, query methods can choose not to use a wrapper type at all.
12
+
The absence of a query result is then indicated by returning `null`.
13
+
Repository methods returning collections, collection alternatives, wrappers, and streams are guaranteed never to return `null` but rather the corresponding empty representation.
14
+
See "`<<repository-query-return-types>>`" for details.
15
+
16
+
[[repositories.nullability.annotations]]
17
+
==== Nullability Annotations
18
+
19
+
You can express nullability constraints for repository methods by using {spring-framework-docs}/core.html#null-safety[Spring Framework's nullability annotations].
20
+
They provide a tooling-friendly approach and opt-in `null` checks during runtime, as follows:
21
+
22
+
* {spring-framework-javadoc}/org/springframework/lang/NonNullApi.html[`@NonNullApi`]: Used on the package level to declare that the default behavior for parameters and return values is, respectively, neither to accept nor to produce `null` values.
23
+
* {spring-framework-javadoc}/org/springframework/lang/NonNull.html[`@NonNull`]: Used on a parameter or return value that must not be `null` (not needed on a parameter and return value where `@NonNullApi` applies).
24
+
* {spring-framework-javadoc}/org/springframework/lang/Nullable.html[`@Nullable`]: Used on a parameter or return value that can be `null`.
25
+
26
+
Spring annotations are meta-annotated with https://jcp.org/en/jsr/detail?id=305[JSR 305] annotations (a dormant but widely used JSR).
27
+
JSR 305 meta-annotations let tooling vendors (such as https://www.jetbrains.com/help/idea/nullable-and-notnull-annotations.html[IDEA], https://help.eclipse.org/latest/index.jsp?topic=/org.eclipse.jdt.doc.user/tasks/task-using_external_null_annotations.htm[Eclipse], and link:https://kotlinlang.org/docs/reference/java-interop.html#null-safety-and-platform-types[Kotlin]) provide null-safety support in a generic way, without having to hard-code support for Spring annotations.
28
+
To enable runtime checking of nullability constraints for query methods, you need to activate non-nullability on the package level by using Spring’s `@NonNullApi` in `package-info.java`, as shown in the following example:
29
+
30
+
.Declaring Non-nullability in `package-info.java`
31
+
====
32
+
[source,java]
33
+
----
34
+
@org.springframework.lang.NonNullApi
35
+
package com.acme;
36
+
----
37
+
====
38
+
39
+
Once non-null defaulting is in place, repository query method invocations get validated at runtime for nullability constraints.
40
+
If a query result violates the defined constraint, an exception is thrown.
41
+
This happens when the method would return `null` but is declared as non-nullable (the default with the annotation defined on the package in which the repository resides).
42
+
If you want to opt-in to nullable results again, selectively use `@Nullable` on individual methods.
43
+
Using the result wrapper types mentioned at the start of this section continues to work as expected: an empty result is translated into the value that represents absence.
44
+
45
+
The following example shows a number of the techniques just described:
Copy file name to clipboardExpand all lines: src/main/asciidoc/repositories.adoc
+2-97
Original file line number
Diff line number
Diff line change
@@ -619,103 +619,6 @@ You can use the types in the first column (or subtypes thereof) as query method
619
619
Alternatively, you can declare `Traversable` (the Vavr `Iterable` equivalent), and we then derive the implementation class from the actual return value.
620
620
That is, a `java.util.List` is turned into a Vavr `List` or `Seq`, a `java.util.Set` becomes a Vavr `LinkedHashSet` `Set`, and so on.
621
621
622
-
[[repositories.nullability]]
623
-
=== Null Handling of Repository Methods
624
-
625
-
As of Spring Data 2.0, repository CRUD methods that return an individual aggregate instance use Java 8's `Optional` to indicate the potential absence of a value.
626
-
Besides that, Spring Data supports returning the following wrapper types on query methods:
627
-
628
-
* `com.google.common.base.Optional`
629
-
* `scala.Option`
630
-
* `io.vavr.control.Option`
631
-
632
-
Alternatively, query methods can choose not to use a wrapper type at all.
633
-
The absence of a query result is then indicated by returning `null`.
634
-
Repository methods returning collections, collection alternatives, wrappers, and streams are guaranteed never to return `null` but rather the corresponding empty representation.
635
-
See "`<<repository-query-return-types>>`" for details.
636
-
637
-
[[repositories.nullability.annotations]]
638
-
==== Nullability Annotations
639
-
640
-
You can express nullability constraints for repository methods by using {spring-framework-docs}/core.html#null-safety[Spring Framework's nullability annotations].
641
-
They provide a tooling-friendly approach and opt-in `null` checks during runtime, as follows:
642
-
643
-
* {spring-framework-javadoc}/org/springframework/lang/NonNullApi.html[`@NonNullApi`]: Used on the package level to declare that the default behavior for parameters and return values is, respectively, neither to accept nor to produce `null` values.
644
-
* {spring-framework-javadoc}/org/springframework/lang/NonNull.html[`@NonNull`]: Used on a parameter or return value that must not be `null` (not needed on a parameter and return value where `@NonNullApi` applies).
645
-
* {spring-framework-javadoc}/org/springframework/lang/Nullable.html[`@Nullable`]: Used on a parameter or return value that can be `null`.
646
-
647
-
Spring annotations are meta-annotated with https://jcp.org/en/jsr/detail?id=305[JSR 305] annotations (a dormant but widely used JSR).
648
-
JSR 305 meta-annotations let tooling vendors (such as https://www.jetbrains.com/help/idea/nullable-and-notnull-annotations.html[IDEA], https://help.eclipse.org/latest/index.jsp?topic=/org.eclipse.jdt.doc.user/tasks/task-using_external_null_annotations.htm[Eclipse], and link:https://kotlinlang.org/docs/reference/java-interop.html#null-safety-and-platform-types[Kotlin]) provide null-safety support in a generic way, without having to hard-code support for Spring annotations.
649
-
To enable runtime checking of nullability constraints for query methods, you need to activate non-nullability on the package level by using Spring’s `@NonNullApi` in `package-info.java`, as shown in the following example:
650
-
651
-
.Declaring Non-nullability in `package-info.java`
652
-
====
653
-
[source,java]
654
-
----
655
-
@org.springframework.lang.NonNullApi
656
-
package com.acme;
657
-
----
658
-
====
659
-
660
-
Once non-null defaulting is in place, repository query method invocations get validated at runtime for nullability constraints.
661
-
If a query result violates the defined constraint, an exception is thrown.
662
-
This happens when the method would return `null` but is declared as non-nullable (the default with the annotation defined on the package in which the repository resides).
663
-
If you want to opt-in to nullable results again, selectively use `@Nullable` on individual methods.
664
-
Using the result wrapper types mentioned at the start of this section continues to work as expected: an empty result is translated into the value that represents absence.
665
-
666
-
The following example shows a number of the techniques just described:
0 commit comments