Skip to content

Commit e25d67a

Browse files
schaudermp911de
authored andcommitted
Improve documentation about name handling.
Closes #616 Original pull request: #617.
1 parent 6fbf8d5 commit e25d67a

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/main/asciidoc/reference/mapping.adoc

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ include::../{spring-data-commons-docs}/object-mapping.adoc[leveloffset=+1]
1717
The conventions are:
1818

1919
* The short Java class name is mapped to the table name in the following manner.
20-
The class `com.bigbank.SavingsAccount` maps to the `savings_account` table name.
20+
The class `com.bigbank.SavingsAccount` maps to the `SAVINGS_ACCOUNT` table name.
21+
The same name mapping is applied for mapping fields to column names.
22+
For example a field `firstName` will be mapped to a column `FIRST_NAME`
23+
You can control this mapping by providing a custom `NamingStrategy`. See <<mapping.configuration>> for more details.
24+
Table and column names that are derived from property or class names are used in SQL statements without quotes by default.
25+
This behaviour can be controlled by setting `R2dbcMappingContext.setForceQuote(true)`.
2126

2227
* Nested objects are not supported.
2328

@@ -39,6 +44,15 @@ By creating your own instance, you can register Spring converters to map specifi
3944

4045
You can configure the `MappingR2dbcConverter` as well as `DatabaseClient` and `ConnectionFactory` by using Java-based metadata. The following example uses Spring's Java-based configuration:
4146

47+
If you set `setForceQuote` of the `R2dbcMappingContext to `true` table and column names derived from classes and properties will be used with database specific quotes.
48+
This means it is ok to use reserved SQL words like for example `order` in these names.
49+
This can be done by overriding `r2dbcMappingContext(Optional<NamingStrategy>)` of `AbstractR2dbcConfiguration`.
50+
Spring Data converts the letter casing of such a name to that form which is also used by the configured database when no quoting is used.
51+
Therefore, you can use unquoted names when creating tables, as long as you don't use keywords or special characters in your names.
52+
For databases adhering to the SQL standard with that respect this means, names will be converted to upper case.
53+
The quoting character used, and the way names get capitalized is controlled by the used `Dialect`.
54+
See <<r2dbc.drivers>> for details how to configure custom dialects.
55+
4256
.@Configuration class to configure R2DBC mapping support
4357
====
4458
[source,java]
@@ -68,6 +82,9 @@ public class MyAppConfig extends AbstractR2dbcConfiguration {
6882

6983
You can add additional converters to the converter by overriding the `r2dbcCustomConversions` method.
7084

85+
You may configure a custom `NamingStrategy` by registering it as a bean.
86+
The `NamingStrategy` controls how the names of classes and properties get converted to the names of tables and columns.
87+
7188
NOTE: `AbstractR2dbcConfiguration` creates a `DatabaseClient` instance and registers it with the container under the name of `databaseClient`.
7289

7390
[[mapping.usage]]
@@ -144,7 +161,7 @@ The following table explains how property types of an entity affect mapping:
144161

145162
|Driver-specific types
146163
|Passthru
147-
|Contributed as simple type be the used `R2dbcDialect`.
164+
|Contributed as simple type by the used `R2dbcDialect`.
148165

149166
|Complex objects
150167
|Target type depends on registered `Converter`.
@@ -173,7 +190,9 @@ Constructor arguments are mapped by name to the values in the retrieved row.
173190
Within the mapping framework it can be applied to constructor arguments.
174191
This lets you use a Spring Expression Language statement to transform a key’s value retrieved in the database before it is used to construct a domain object.
175192
In order to reference a column of a given row one has to use expressions like: `@Value("#root.myProperty")` where root refers to the root of the given `Row`.
176-
* `@Column`: Applied at the field level to describe the name of the column as it is represented in the row, allowing the name to be different than the field name of the class.
193+
* `@Column`: Applied at the field level to describe the name of the column as it is represented in the row, allowing the name to be different from the field name of the class.
194+
Names specified with a `@Column` annotation are always quoted when used in SQL statements.
195+
For most databases this means these names are case-sensitive. It also means you may use special characters in these names, although this is not recommended since it may cause problems with other tools.
177196
* `@Version`: Applied at field level is used for optimistic locking and checked for modification on save operations.
178197
The value is `null` (`zero` for primitive types) is considered as marker for entities to be new.
179198
The initially stored value is `zero` (`one` for primitive types).

src/main/asciidoc/reference/r2dbc-core.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ This approach lets you use the standard `io.r2dbc.spi.ConnectionFactory` instanc
182182
Spring Data R2DBC supports drivers through R2DBC's pluggable SPI mechanism.
183183
You can use any driver that implements the R2DBC spec with Spring Data R2DBC.
184184
Since Spring Data R2DBC reacts to specific features of each database, it requires a `Dialect` implementation otherwise your application won't start up.
185-
Spring Data R2DBC ships with dialect impelemtations for the following drivers:
185+
Spring Data R2DBC ships with dialect implementations for the following drivers:
186186

187187
* https://github.com/r2dbc/r2dbc-h2[H2] (`io.r2dbc:r2dbc-h2`)
188188
* https://github.com/mariadb-corporation/mariadb-connector-r2dbc[MariaDB] (`org.mariadb:r2dbc-mariadb`)

src/main/asciidoc/reference/r2dbc-repositories.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ interface ReactivePersonRepository extends ReactiveSortingRepository<Person, Lon
118118
The annotated query uses native bind markers, which are Postgres bind markers in this example.
119119
====
120120

121+
Note that the columns of a select used in a `@Query` annotation must match the names generated by the `NamingStrategy` for the respective property.
122+
If for a property no matching column is provided in a select, that property is not set. If that property is required by the persistence constructor, `null` is provided as a value or the default value for primitive types.
123+
121124
The following table shows the keywords that are supported for query methods:
122125

123126
[cols="1,2,3", options="header", subs="quotes"]

0 commit comments

Comments
 (0)