#367 - Polishing.

Simplified the NamingStrategy by replacing parts of it with Table and Column annotations.
This commit is contained in:
Jens Schauder
2018-05-24 09:38:07 +02:00
parent 3248cda95c
commit 0a9a8f4e60
3 changed files with 7 additions and 11 deletions

View File

@@ -14,9 +14,9 @@ This example demonstrate basic usage of JDBC based repositories.
This example demonstrates various ways to bend what the standard mapping of Spring Data JDBC can do.
* `AggregateContext.idSetting()` registers an `ApplicationListener` to implement a custom id generation strategy for `LegoSet` and `Manual`.
* `AggregateConfiguration.idSetting()` registers an `ApplicationListener` to implement a custom id generation strategy for `LegoSet` and `Manual`.
* `AggregateContext.namingStrategy()` registers a custom `NamingStrategy` in order to map property and class names to database columns and tables.
* `AggregateConfiguration.namingStrategy()` registers a custom `NamingStrategy` in order to map property and class names to database columns and tables.
* The `minimumAge` and `maximumAge` properties show a way to use types which don't have direct mapping to a SQL type, by marking them with `@Transient` and having converted properties for Spring Data JDBC to use.

View File

@@ -38,6 +38,7 @@ import org.springframework.lang.Nullable;
@Configuration
@EnableJdbcRepositories
public class AggregateConfiguration {
final AtomicInteger id = new AtomicInteger(0);
@Bean
@@ -67,13 +68,9 @@ public class AggregateConfiguration {
@Bean
public NamingStrategy namingStrategy() {
Map<String, String> tableAliases = new HashMap<String, String>();
tableAliases.put("manual", "handbuch");
Map<String, String> columnAliases = new HashMap<String, String>();
columnAliases.put("lego_set.int_maximum_age", "max_age");
columnAliases.put("lego_set.int_minimum_age", "min_age");
columnAliases.put("handbuch.id", "handbuch_id");
Map<String, String> reverseColumnAliases = new HashMap<String, String>();
reverseColumnAliases.put("manual", "handbuch_id");
@@ -91,11 +88,6 @@ public class AggregateConfiguration {
return columnAliases.computeIfAbsent(key, __ -> defaultName);
}
@Override
public String getTableName(Class<?> type) {
return tableAliases.computeIfAbsent(NamingStrategy.super.getTableName(type), key -> key);
}
@Override
public String getReverseColumnName(JdbcPersistentProperty property) {
return reverseColumnAliases.computeIfAbsent(property.getName(),

View File

@@ -18,6 +18,8 @@ package example.springdata.jdbc.basics.aggregate;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.jdbc.core.mapping.Column;
import org.springframework.data.jdbc.core.mapping.Table;
/**
* A manual instructing how to assemble a {@link LegoSet}.
@@ -25,8 +27,10 @@ import org.springframework.data.annotation.Id;
* @author Jens Schauder
*/
@Data
@Table("handbuch")
public class Manual {
@Column("handbuch_id")
private @Id Long id;
private String author, text;