#367 - Polishing.
Simplified the NamingStrategy by replacing parts of it with Table and Column annotations.
This commit is contained in:
@@ -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.
|
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.
|
* 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.
|
||||||
|
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ import org.springframework.lang.Nullable;
|
|||||||
@Configuration
|
@Configuration
|
||||||
@EnableJdbcRepositories
|
@EnableJdbcRepositories
|
||||||
public class AggregateConfiguration {
|
public class AggregateConfiguration {
|
||||||
|
|
||||||
final AtomicInteger id = new AtomicInteger(0);
|
final AtomicInteger id = new AtomicInteger(0);
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@@ -67,13 +68,9 @@ public class AggregateConfiguration {
|
|||||||
@Bean
|
@Bean
|
||||||
public NamingStrategy namingStrategy() {
|
public NamingStrategy namingStrategy() {
|
||||||
|
|
||||||
Map<String, String> tableAliases = new HashMap<String, String>();
|
|
||||||
tableAliases.put("manual", "handbuch");
|
|
||||||
|
|
||||||
Map<String, String> columnAliases = new HashMap<String, String>();
|
Map<String, String> columnAliases = new HashMap<String, String>();
|
||||||
columnAliases.put("lego_set.int_maximum_age", "max_age");
|
columnAliases.put("lego_set.int_maximum_age", "max_age");
|
||||||
columnAliases.put("lego_set.int_minimum_age", "min_age");
|
columnAliases.put("lego_set.int_minimum_age", "min_age");
|
||||||
columnAliases.put("handbuch.id", "handbuch_id");
|
|
||||||
|
|
||||||
Map<String, String> reverseColumnAliases = new HashMap<String, String>();
|
Map<String, String> reverseColumnAliases = new HashMap<String, String>();
|
||||||
reverseColumnAliases.put("manual", "handbuch_id");
|
reverseColumnAliases.put("manual", "handbuch_id");
|
||||||
@@ -91,11 +88,6 @@ public class AggregateConfiguration {
|
|||||||
return columnAliases.computeIfAbsent(key, __ -> defaultName);
|
return columnAliases.computeIfAbsent(key, __ -> defaultName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getTableName(Class<?> type) {
|
|
||||||
return tableAliases.computeIfAbsent(NamingStrategy.super.getTableName(type), key -> key);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getReverseColumnName(JdbcPersistentProperty property) {
|
public String getReverseColumnName(JdbcPersistentProperty property) {
|
||||||
return reverseColumnAliases.computeIfAbsent(property.getName(),
|
return reverseColumnAliases.computeIfAbsent(property.getName(),
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ package example.springdata.jdbc.basics.aggregate;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import org.springframework.data.annotation.Id;
|
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}.
|
* A manual instructing how to assemble a {@link LegoSet}.
|
||||||
@@ -25,8 +27,10 @@ import org.springframework.data.annotation.Id;
|
|||||||
* @author Jens Schauder
|
* @author Jens Schauder
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
|
@Table("handbuch")
|
||||||
public class Manual {
|
public class Manual {
|
||||||
|
|
||||||
|
@Column("handbuch_id")
|
||||||
private @Id Long id;
|
private @Id Long id;
|
||||||
private String author, text;
|
private String author, text;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user