Skip to content

Commit 02ed197

Browse files
grubeninspekteurmp911de
authored andcommitted
Recommend Java records instead of Lombok for class-based projections.
Closes #2793 Original pull request: #2794
1 parent d579be5 commit 02ed197

File tree

1 file changed

+2
-36
lines changed

1 file changed

+2
-36
lines changed

Diff for: src/main/asciidoc/repository-projections.adoc

+2-36
Original file line numberDiff line numberDiff line change
@@ -237,46 +237,12 @@ The following example shows a projecting DTO:
237237
====
238238
[source, java]
239239
----
240-
class NamesOnly {
241-
242-
private final String firstname, lastname;
243-
244-
NamesOnly(String firstname, String lastname) {
245-
246-
this.firstname = firstname;
247-
this.lastname = lastname;
248-
}
249-
250-
String getFirstname() {
251-
return this.firstname;
252-
}
253-
254-
String getLastname() {
255-
return this.lastname;
256-
}
257-
258-
// equals(…) and hashCode() implementations
240+
record NamesOnly(String firstname, String lastname) {
259241
}
260242
----
261243
====
262244

263-
[TIP]
264-
.Avoid boilerplate code for projection DTOs
265-
====
266-
You can dramatically simplify the code for a DTO by using https://projectlombok.org[Project Lombok], which provides an `@Value` annotation (not to be confused with Spring's `@Value` annotation shown in the earlier interface examples).
267-
If you use Project Lombok's `@Value` annotation, the sample DTO shown earlier would become the following:
268-
269-
[source,java]
270-
----
271-
@Value
272-
class NamesOnly {
273-
String firstname, lastname;
274-
}
275-
----
276-
277-
Fields are `private final` by default, and the class exposes a constructor that takes all fields and automatically gets `equals(…)` and `hashCode()` methods implemented.
278-
279-
====
245+
Records are ideal DTOs since they adhere to value semantics (all fields are private final, `equals(…)` and `hashCode()` are provided by default), but you are free to use any class with a constructor listing the fields to be retrieved.
280246

281247
ifdef::repository-projections-trailing-dto-fragment[]
282248
include::{repository-projections-trailing-dto-fragment}[]

0 commit comments

Comments
 (0)