Skip to content

Commit bddeba7

Browse files
committed
DATACMNS-1190 - Polishing the polishing.
Slight rewording. Extracted path to Spring Framework JavaDoc into variable. Original pull request: #253.
1 parent e1f1e9c commit bddeba7

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

Diff for: src/main/asciidoc/repositories.adoc

+22-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
:spring-framework-docs: http://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/
1+
:spring-framework-docs: http://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference
2+
:spring-framework-javadoc: https://docs.spring.io/spring/docs/{springVersion}/javadoc-api
23

34
[[repositories]]
45
= Working with Spring Data Repositories
@@ -227,26 +228,21 @@ Besides that, Spring Data supports to return other wrapper types on query method
227228

228229
Alternatively query methods can choose not to use a wrapper type at all.
229230
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.
231233

232234
[[repositories.nullability.annotations]]
233235
==== Nullability annotations
234236

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:
237239

238-
* https://docs.spring.io/spring-framework/docs/{springVersion}/javadoc-api/org/springframework/lang/NonNull.html[`@NonNull`]
239-
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`
240242
(not needed on parameter and return value where `@NonNullApi` applies).
241-
242-
* https://docs.spring.io/spring-framework/docs/{springVersion}/javadoc-api/org/springframework/lang/Nullable.html[`@Nullable`]
243-
annotation where a specific parameter or return value can be `null`.
244-
245-
* https://docs.spring.io/spring-framework/docs/{springVersion}/javadoc-api/org/springframework/lang/NonNullApi.html[`@NonNullApi`]
246-
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`.
247244

248245
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-
250246
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`:
251247

252248
.Declaring non-nullability in `package-info.java`
@@ -260,39 +256,39 @@ package com.acme;
260256

261257
Once non-null defaulting is in place, repository query method invocations will get validated at runtime for nullability constraints.
262258
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.
265260
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.
266261

267262
.Using different nullability constraints
268263
====
269264
[source, java]
270265
----
271-
package com.acme;
266+
package com.acme; <1>
272267
273268
import org.springframework.lang.Nullable;
274269
275270
interface UserRepository extends Repository<User, Long> {
276271
277-
User getByEmailAddress(EmailAddress emailAddress); <1>
272+
User getByEmailAddress(EmailAddress emailAddress); <2>
278273
279274
@Nullable
280-
User findByEmailAddess(@Nullable EmailAddress emailAdress); <2>
275+
User findByEmailAddress(@Nullable EmailAddress emailAdress); <3>
281276
282-
Optional<User> findOptionalByEmailAddress(EmailAddress emailAddress); <3>
277+
Optional<User> findOptionalByEmailAddress(EmailAddress emailAddress); <4>
283278
}
284279
----
285-
<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`.
288284
====
289285

290286
[[repositories.nullability.kotlin]]
291287
==== Nullability in Kotlin-based repositories
292288

293289
Kotlin has the definition of https://kotlinlang.org/docs/reference/null-safety.html[nullability constraints]
294290
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.
296292
Spring Data repositories use the language mechanism to define those constraints to apply the same runtime checks:
297293

298294
.Using nullability constraints on Kotlin repositories
@@ -301,9 +297,9 @@ Spring Data repositories use the language mechanism to define those constraints
301297
----
302298
interface UserRepository : Repository<User, String> {
303299
304-
fun findByUsername(username: String): User <1>
300+
fun findByUsername(username: String): User <1>
305301
306-
fun findByFirstname(firstname: String?): User? <2>
302+
fun findByFirstname(firstname: String?): User? <2>
307303
}
308304
----
309305
<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
582578
[[repositories.query-async]]
583579
=== Async query results
584580

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.
586582

587583
====
588584
[source, java]

0 commit comments

Comments
 (0)