Skip to content

Commit 08e7f5a

Browse files
committed
Clearly document that DataClassRowMapper supports Java records
Closes gh-29814
1 parent 5f458e2 commit 08e7f5a

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/core/BeanPropertyRowMapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@
7373
* {@code true}. See {@link #setPrimitivesDefaultedForNullValue(boolean)} for
7474
* details.
7575
*
76+
* <p>If you need to map to a target class which has a <em>data class</em> constructor
77+
* &mdash; for example, a Java {@code record} or a Kotlin {@code data} class &mdash;
78+
* use {@link DataClassRowMapper} instead.
79+
*
7680
* <p>Please note that this class is designed to provide convenience rather than
7781
* high performance. For best performance, consider using a custom {@code RowMapper}
7882
* implementation.

spring-jdbc/src/main/java/org/springframework/jdbc/core/DataClassRowMapper.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,29 @@
3232
* {@link RowMapper} implementation that converts a row into a new instance
3333
* of the specified mapped target class. The mapped target class must be a
3434
* top-level class or {@code static} nested class, and it may expose either a
35-
* data class constructor with named parameters corresponding to column names
36-
* or classic bean property setter methods with property names corresponding to
37-
* column names (or even a combination of both).
35+
* <em>data class</em> constructor with named parameters corresponding to column
36+
* names or classic bean property setter methods with property names corresponding
37+
* to column names (or even a combination of both).
38+
*
39+
* <p>The term "data class" applies to Java <em>records</em>, Kotlin <em>data
40+
* classes</em>, and any class which has a constructor with named parameters
41+
* that are intended to be mapped to corresponding column names.
42+
*
43+
* <p>When combining a data class constructor with setter methods, any property
44+
* mapped successfully via a constructor argument will not be mapped additionally
45+
* via a corresponding setter method. This means that constructor arguments take
46+
* precedence over property setter methods.
3847
*
3948
* <p>Note that this class extends {@link BeanPropertyRowMapper} and can
4049
* therefore serve as a common choice for any mapped target class, flexibly
4150
* adapting to constructor style versus setter methods in the mapped class.
4251
*
52+
* <p>Please note that this class is designed to provide convenience rather than
53+
* high performance. For best performance, consider using a custom {@code RowMapper}
54+
* implementation.
55+
*
4356
* @author Juergen Hoeller
57+
* @author Sam Brannen
4458
* @since 5.3
4559
* @param <T> the result type
4660
*/

0 commit comments

Comments
 (0)